sreq
sreq is a simple, user-friendly and concurrent safe HTTP request library for Go, inspired by Python requests .
![License](https://img.shields.io/github/license/winterssy/sreq.svg)
Notes
sreq
now is under a beta state, use the latest version if you want to try it.- The author does not provide any backward compatible guarantee at present.
Features
- Requests-style APIs.
- GET, POST, PUT, PATCH, DELETE, etc.
- Easy set query params, headers and cookies.
- Easy send form, JSON or multipart payload.
- Easy set basic authentication or bearer token.
- Easy set proxy.
- Easy set context.
- Backoff retry mechanism.
- Automatic cookies management.
- Request and response interceptors.
- Reverse proxy.
- Rate limiter for handling outbound requests.
- Easy decode responses, raw data, text representation and unmarshal the JSON-encoded data.
- Export curl command.
- Friendly debugging.
- Concurrent safe.
Install
go get -u github.com/winterssy/sreq
go get -u github.com/winterssy/sreq@master
Usage
import "github.com/winterssy/sreq"
Quick Start
The usages of sreq
are very similar to net/http
, you can switch from it to sreq
easily. For example, if your HTTP request code like this:
resp, err := http.Get("https://www.google.com")
Use sreq
you just need to change your code like this:
resp, err := sreq.Get("https://www.google.com").Raw()
You have two convenient ways to access the APIs of sreq
.
const (
url = "http://httpbin.org/get"
userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
)
query := sreq.Params{
"k1": "v1",
"k2": "v2",
}
client := sreq.New()
req, err := sreq.NewRequest("GET", url,
sreq.WithQuery(query),
sreq.WithUserAgent(userAgent),
)
if err != nil {
panic(err)
}
err = client.
Do(req).
EnsureStatusOk().
Verbose(ioutil.Discard, false)
if err != nil {
panic(err)
}
err = client.
Get(url,
sreq.WithQuery(query),
sreq.WithUserAgent(userAgent),
).
EnsureStatusOk().
Verbose(os.Stdout, true)
if err != nil {
panic(err)
}
Code examples
License
MIT