https://github.com/nmrshll/go-httpclient-middl
Add middleware to your HTTP clients in Go
https://github.com/nmrshll/go-httpclient-middl
client go golang http http-client middleware
Last synced: 10 months ago
JSON representation
Add middleware to your HTTP clients in Go
- Host: GitHub
- URL: https://github.com/nmrshll/go-httpclient-middl
- Owner: nmrshll
- License: mit
- Created: 2018-09-22T11:26:49.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-09-22T12:55:14.000Z (over 7 years ago)
- Last Synced: 2024-06-20T12:52:49.631Z (over 1 year ago)
- Topics: client, go, golang, http, http-client, middleware
- Language: Go
- Size: 9.77 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.com/nmrshll/go-httpclient-middl)
[](https://goreportcard.com/report/github.com/nmrshll/go-httpclient-middl)
# go-httpclient-middl
Add middleware to your HTTP clients in Go
## Why ?
If you need an HTTP client that automatically logs requests, tracks metrics, joins a token with each request, validates HTTP status codes, or does any other custom action for every request/reponse, this library might be for you.
## How ?
Download the library using `go get -u github.com/nmrshll/go-httpclient-middl`
Then use it this way:
[embedmd]:# (.docs/examples/quickstart.go /func main/ $)
```go
func main() {
httpClient := http.Client{Timeout: 30 * time.Second}
client, err := middl.NewClient(&httpClient)
if err != nil {
log.Fatal(err)
}
// add middleware to you client (classic examples provided in this library or custom)
client.UseMiddleware(logger.New())
client.UseMiddleware(statusvalidator.New())
// then do your requests as usual
resp, err := client.Get("https://google.com")
if err != nil {
log.Fatal(err)
}
if resp == nil {
log.Fatalf("no response from server")
} // else
defer resp.Body.Close()
// do something with the response here
}
```