https://github.com/xgfone/go-http-client
A chained go HTTP client.
https://github.com/xgfone/go-http-client
client go-client go-http-client http-client http-request httpclient request
Last synced: about 1 month ago
JSON representation
A chained go HTTP client.
- Host: GitHub
- URL: https://github.com/xgfone/go-http-client
- Owner: xgfone
- License: apache-2.0
- Created: 2021-08-04T16:03:45.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2025-04-20T06:06:28.000Z (3 months ago)
- Last Synced: 2025-04-20T07:19:20.568Z (3 months ago)
- Topics: client, go-client, go-http-client, http-client, http-request, httpclient, request
- Language: Go
- Homepage:
- Size: 86.9 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# A chained go HTTP client
[](https://github.com/xgfone/go-http-client/actions/workflows/go.yml)
[](https://pkg.go.dev/github.com/xgfone/go-http-client)
[](https://raw.githubusercontent.com/xgfone/go-http-client/master/LICENSE)

## Install
```shell
$ go get -u github.com/xgfone/go-http-client
```## Example
```go
package mainimport (
"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"`
}// client.SetBaseURL("http://localhost:12345").Get("/path")
err := client.Get("http://127.0.0.1/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)
}
}
```There are also some convenient functions, such as `GetContext`, `PutContext`, `PostContext`, `DeleteContext`, etc.
```go
package mainimport (
"context"
"fmt"httpclient "github.com/xgfone/go-http-client"
)func main() {
var result struct {
Username string `json:"username"`
Password string `json:"password"`
}
err := httpclient.GetContext(context.Background(), "http://127.0.0.1/json_data", &result)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(result)
}
}
```