Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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 2 months 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 (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-04-08T14:00:55.000Z (10 months ago)
- Last Synced: 2024-10-30T20:49:07.112Z (3 months ago)
- Topics: client, go-client, go-http-client, http-client, http-request, httpclient, request
- Language: Go
- Homepage:
- Size: 50.8 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-http-client [![Build Status](https://github.com/xgfone/go-http-client/actions/workflows/go.yml/badge.svg)](https://github.com/xgfone/go-http-client/actions/workflows/go.yml) [![GoDoc](https://pkg.go.dev/badge/github.com/xgfone/go-http-client)](https://pkg.go.dev/github.com/xgfone/go-http-client) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg?style=flat-square)](https://raw.githubusercontent.com/xgfone/go-http-client/master/LICENSE)
A chained go HTTP client supporting `Go1.8+`.
## 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"`
}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.
```go
package mainimport (
"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)
}
}
```