easonlin404 / esrest

✨ Easy, elegant, fluent HTTP client API for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

esrest

Easy, elegant, fluent HTTP client API for Go

Travis branch Codecov branch Go Report Card GoDoc

Features

  • Support basic HTTP GET/POST/PUT/DELETE/HEAD in a fluent style
  • Only use Body fluent function to send payload(JSON/string/slice/pointer/map)
  • Basic Authentication
  • Request timeout
  • Debug with customized Logger
  • Receive unmarshal JSON
  • Multipart request
  • Context
  • application/x-www-form-urlencoded
  • todo

Installation

$ go get -u github.com/easonlin404/esrest

Usage

GET/POST/PUT/DELETE

res, err := esrest.New().Get("http://httpbin.org/get").Do()

Add header (Default ContentType is "application/json")

res, err := esrest.New().
		    Get("http://httpbin.org/get").
		    Header("MyHader", "headvalue").
		    Do()

Sending JSON payload use Body chain method same as other:

//JSON struct
res, err := esrest.New().
		    Post("http://httpbin.org/post").
		    Body(struct {
                 		Message string `json:"message"`
                 	}{"ok"}).
		    Do()
//pointer to JSON struct
res, err := esrest.New().
		    Post("http://httpbin.org/post").
		    Body(&struct {
                 		Message string `json:"message"`
                 	}{"ok"}).
		    Do()		    
//slice
res, err := esrest.New().
		    Post("http://httpbin.org/post").
		    Body([]byte(`{"message":"ok"}`)).
		    Do()
		    
//string
res, err := esrest.New().
		    Post("http://httpbin.org/post").
		    Body(string(`{"message":"ok"}`)).
		    Do()
		    
//map
m := map[string]interface{}{
		"message": "ok",
	}
	
res, err := esrest.New().
		    Post("http://httpbin.org/post").
		    Body(m).
		    Do()

Add Query parameter:

res, err := esrest.New().
		    Get("http://httpbin.org/get").
		    Query("Param1", "value").
		    Do()

Receive unmarshal JSON:

json := &struct {
		Message string `json:"message"`
	}{}
res, err := esrest.New().
		    Post("http://httpbin.org/post").
		    DoJson(json)

Basic Authentication:

res, err := esrest.New().
		    BasicAuth("user", "password").
		    Get("http://httpbin.org/get").
		    Do()

Debug:

Print http request and response debug payload at stdout, and you also can use your logger by using Logger chain

mylogger:=log.New(os.Stdout, "", log.LstdFlags)

res, err := esrest.New().
		    Debug(true).
		    Logger(mylogger).  //optional
		    Get("http://httpbin.org/get").
		    Do()

About

✨ Easy, elegant, fluent HTTP client API for Go

License:MIT License


Languages

Language:Go 100.0%