request
A concise HTTP request client for Go. It provides elegant and chainalbe API to make you request with happiness.
Installation
go get -u github.com/DavidCai1993/request
Documentation
API documentation can be found here: https://godoc.org/github.com/DavidCai1993/request
Usage
Example:
json, err = request.
Post("http://mysite.com").
Timeout(30*time.Second).
Send(map[string]string{"name": "David"}).
Set("X-HEADER-KEY", "foo").
Accept("application/json").
JSON()
type MyResult struct {
Code int `json:"code"`
Error string `json:"error"`
Data map[string]interface{} `json:"data"`
}
json, err = request.
Get("http://mysite.com").
JSON(new(MyResult))
GET:
res, err = request.
Get("http://mysite.com").
End()
Cookie:
text, err = request.
Get("http://mysite.com/get").
Cookie(&http.Cookie{Name: "name", Value: "David"}).
Text()
Basic Authentication
json, err = request.
Get("http://mysite.com/somebooks").
Auth("name", "passwd").
JSON()
Form with Attachments
json, err = request.
Post("http://mysite.com/form").
Field(url.Values{"key": []string{"value1"}}).
Attach("test.md", "./README.md", "README.md").
JSON()
Proxy
json, err = request.
Get("http://mysite.com/somebooks").
Proxy("http://myproxy.com:8080").
JSON()
Convert to http.Request instance
req, err = request.
Post("http://mysite.com/form").
Auth("name", "passwd").
Req()