A golang http request library for human
- Light weight
- Simple
- Easy play with JSON and XML
- Easy for debug and logging
- Easy file uploads and downloads
- Easy manage cookie
- Easy set up proxy
- Easy set timeout
- Easy customize http client
go get github.com/imroc/reqOnly url is required, others are optional, like headers, params, files or body etc
func Get(url string, v ...interface{}) (*Req, error)
func Post(url string, v ...interface{}) (*Req, error)
......The struct Req hold both request and response infomation.
Basic
Set Header
Set Param
Set Body
Debug & Logging
ToJSON & ToXML
Get *http.Response
Upload
Download
Cookie
Set Timeout
Set Proxy
Customize Client
header := req.Header{
"Accept": "application/json",
"Authorization": "Basic YWRtaW46YWRtaW4=",
}
param := req.Param{
"name": "imroc",
"cmd": "add",
}
// only url is required, others are optional.
r, err = req.Post("http://foo.bar/api", header, param)
if err != nil {
log.Fatal(err)
}
r.ToJSON(&foo) // response => struct/map
log.Printf("%+v", r) // print info (try it, you may surprise) use req.Header
authHeader := req.Header{
"Accept": "application/json",
"Authorization": "Basic YWRtaW46YWRtaW4=",
}
req.Get("https://www.baidu.com", authHeader, req.Header{"User-Agent": "V1.1"})use http.Header
header := make(http.Header)
header.Set("Accept", "application/json")
r, err := req.Get("https://www.baidu.com", header)use req.Param
param := req.Param{
"id": "imroc",
"pwd": "roc",
}
req.Get("http://foo.bar/api", param) // http://foo.bar/api?id=imroc&pwd=roc
req.Post(url, param) // body => id=imroc&pwd=rocput string, []byte and io.Reader as body directly.
req.Post(url, "id=roc&cmd=query")put xml and json body
req.Post(url, req.BodyJSON(&foo))
req.Post(url, req.BodyXML(&bar))Enable debug mode
req.Debug = true
req.Get(url) // it will print the debug info, try it :)Use %+v format to print debug info
r, _ := req.Post(url, header, param)
log.Printf("%+v", r)Use %v format to print info simple
r, _ := req.Get(url, param)
log.Printf("%v", r) // GET http://foo.bar/api?name=roc&cmd=add {"code":"0","msg":"success"}and the %-v format is similar to %v, the only difference is that it always keep the content in one line, it is useful while logging.
r, _ := req.Get(url)
r.ToJSON(&foo)
r, _ = req.Post(url, req.BodyXML(&bar))
r.ToXML(&baz)// func (r *Req) Response() *http.Response
r, _ := req.Get(url)
resp := r.Response()
fmt.Println(resp.StatusCode)Use req.File to match files
req.Post(url, req.File("imroc.png"), req.File("/Users/roc/Pictures/*.png"))Use req.FileUpload to fully control
file, _ := os.Open("imroc.png")
req.Post(url, req.FileUpload{
File: file,
FieldName: "file",
FileName: "avatar.png",
})r, _ := req.Get(url)
r.ToFile("imroc.png")By default, the underlying *http.Client will manage your cookie(send cookie header to server automatically if server has set a cookie for you), you can disable it by calling this function :
req.EnableCookie(false)and you can set cookie in request just using *http.Cookie
cookie := new(http.Cookie)
......
req.Get(url, cookie)req.SetTimeout(50 * time.Second)By default, req use proxy from system environment if http_proxy or https_proxy is specified, you can set a custom proxy or disable it by set nil
req.SetProxy(func(r *http.Request) (*url.URL, error) {
if strings.Contains(r.URL.Hostname(), "google") {
return url.Parse("http://my.vpn.com:23456")
}
return nil, nil
})Set a simple proxy (use fixed proxy url for every request)
req.SetProxyUrl("http://my.proxy.com:23456")Use req.SetClient to change the default underlying *http.Client
req.SetClient(client)Only specify client for some request
client := &http.Client{Timeout: 30 * time.Second}
req.Get(url, client)Change some properties of default client you want
req.Client().Jar, _ = cookiejar.New(nil)
trans, _ := req.Client().Transport.(*http.Transport)
trans.MaxIdleConns = 20
trans.TLSHandshakeTimeout = 20 * time.Second
trans.DisableKeepAlives = true
trans.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}