A chained go HTTP client supporting Go1.8+
.
$ go get -u github.com/xgfone/go-http-client
package main
import (
"context"
"fmt"
httpclient "github.com/xgfone/go-http-client"
)
func main() {
// Create a new HTTP Client instead of the default.
client := httpclient.Clone().SetContentType("application/json").AddAccept("application/json")
// (Optional): Set other options.
// client.
// SetReqBodyEncoder(httpclient.EncodeData).
// SetResponseHandler2xx(responseHandler2xx).
// SetResponseHandler4xx(responseHandler4xx).
// SetResponseHandler5xx(responseHandler5xx)
var result struct {
Username string `json:"username"`
Password string `json:"password"`
}
err := client.
Get("http://127.0.0.1/path"). // Or SetBaseURL("http://localhost:12345").Get("/path").
AddAccept("application/xml").
AddHeader("X-Header1", "value1").
SetHeader("X-Header2", "value2").
AddQuery("querykey1", "queryvalue1").
SetQuery("querykey2", "queryvalue2").
// Use the encoder referring to the request header "Content-Type" to encode the body.
SetBody(map[string]string{"username": "xgfone", "password": "123456"}).
// Also use other body types:
// SetBody([]byte(`this is the body as the type []byte`)).
// SetBody("this is the body as the type string").
// SetBody(bytes.NewBufferString("this is the body as the type io.Reader")).
// Use the decoder referring to the response header "Content-Type" to decode the body into result.
Do(context.Background(), &result).
Unwrap() // Close the response body and return the inner error.
if err != nil {
fmt.Println(err)
} else {
fmt.Println(result)
}
}
If the request is about JSON, you maybe use these convenient functions, such as GetJSON
, PutJSON
, PostJSON
, DeleteJSON
, etc.
package main
import (
"fmt"
httpclient "github.com/xgfone/go-http-client"
)
func main() {
var result struct {
Username string `json:"username"`
Password string `json:"password"`
}
err := httpclient.GetJSON("http://127.0.0.1/json_data", &result)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(result)
}
}