There are three objects in grequest:
- Client: is http.Client, different requests can use the same http.Client, or not, it depends on your
- Request: have http.Request, URL, method, cookie, RetryConfig, etc.
- Result: have http.Response, error, retry log, cost time, etc.
- Method chaining
- Reuse http.Client: means you can use the connection pool of http.Client
- Set: set header, method, cookie, etc.
- JSON: Post JSON request, receive JSON response
- Timeout: set request timeout
- Retry: set retry HTTP status, retry times and retry interval
- BasicAuth: set authentication header
see request_test.go for more examples.
//new a client with 1s timeout
client := NewClient(time.Second)
// send request
resp, body, err := NewRequest(client).Get("https://github.com/").send()
resp, body, err := NewRequest(client).
SetHeader("TestHeader", "header"). //set a new header
AddHeader("TestHeader", "header2"). //add(not replace) a header which already exist
SetRetry(1, time.Millisecond*50, nil, 500, 504). //using retry, try again 50ms later when last request return a http status between 500 and 504
DisableKeepAlive().
Get("http://test.com").
Send().
Response()
type Req struct {
Name string
Age int
}
respJson := make(map[string]string, 0)
resp, body, err := NewRequest(client).
SetRetry(1, time.Millisecond*50, []int{500,502}, -1, -1). //using retry,try again 50ms later when last request return a http status is 500 or 502
Post("http://test.com").
SendJson(&Req{Name:"test", Age:10}).
JsonTo(respJson)
//new a default client with 3s timeout
SetDefaultClient(NewClient(time.Second * 3))
// send request using default client
resp, body, err := NewRequest(DefaultClient()).Get("http://test.com").send()