https://github.com/creatcodebuild/h
http for Gophers, and human
https://github.com/creatcodebuild/h
Last synced: 9 months ago
JSON representation
http for Gophers, and human
- Host: GitHub
- URL: https://github.com/creatcodebuild/h
- Owner: CreatCodeBuild
- License: mit
- Created: 2018-06-02T00:22:48.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-06-12T08:27:37.000Z (over 7 years ago)
- Last Synced: 2025-02-15T22:43:10.981Z (11 months ago)
- Language: Go
- Size: 9.77 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# H
__http for Gophers, and human__
Go std lib `net/http` is easy to use, but verbose. Package `h` provides a simply yet flexible API for Gophers and other human. Readable, maintainable, debuggable, extendable and flexible API is the goal.
## Usage
### Basic
```go
client := NewClient().
SetBaseURL("baseURL").
SetHeader("Header1", "Value").
SetHeader("Header2", "Value").
Use(func(r *Request, res *http.Response, err error) (*http.Response, error) {
if res.StatusCode == 500 {
log.Fatal(500)
}
}).
SetTimeout(5 * time.Second)
res, err := client.Request(http.MethodGet, "www.google.com").
SetHeader("Header3", "Value").
SetBody(strings.NewReader("123")).
Run()
```
This returns a standard `(http.Response, error)` pair, same return signature as `http.Client.Do` function.
As you can see, a convenient method chaining API is provided.
### Plugins
The power of `h` comes in with plugins. `h` itself has less than 120 lines of code. But it has a extendable design.
`h/hplug` package provides several powerful plugins, including retries. Please see [hplug examples](plugin/retry/retry_test.go).