Httplib Module
Client Request
Similar to Curl, httplib is used to simulate http requests sent by clients. Similar to jQuery, it supports method chaining. It’s easy to use. It can be installed by:
go get github.com/astaxie/beego/httplib
Basic Usage
Import package:
import (
"github.com/astaxie/beego/httplib"
)
Initialize request method and url:
req := httplib.Get("http://beego.wiki/")
Send the request and retrieve the data in the response:
str, err := res.String()
if err != nil {
t.Fatal(err)
}
fmt.Println(str)
Method Functions
httplib supports these methods:
Get(url string)
Post(url string)
Put(url string)
Delete(url string)
Head(url string)
Debug Output
Enable debug information output:
req.Debug(true)
Then it will output debug information:
httplib.Get("http://beego.wiki/").Debug(true).Response()
// Output
GET / HTTP/0.0
Host: beego.wiki
User-Agent: beegoServer
HTTPS Request
If the requested scheme is https, we need to set TLS of client:
req.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
Set Timeout
Can set request timeout and data reading timeout by:
req.SetTimeout(connectTimeout, readWriteTimeout)
It is a function of request object. So it can be done like this:
httplib.Get("http://beego.wiki/").SetTimeout(100 * time.Second, 30 * time.Second).Response()
Set Request Params
For Put or Post requests, we may need to send parameters. Parameters can be set like:
req := httplib.Post("http://beego.wiki/")
req.Param("username","astaxie")
req.Param("password","123456")
Send big data
To simulate file uploading or to send big data, one can use the Body
function:
req := httplib.Post("http://beego.wiki/")
bt,err:=ioutil.ReadFile("hello.txt")
if err!=nil{
log.Fatal("read file err:",err)
}
req.Body(bt)
Set header
To simulate header values, e.g.:
Accept-Encoding:gzip,deflate,sdch
Host:beego.wiki
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36
Can use Header
function:
req := httplib.Post("http://beego.wiki/")
req.Header("Accept-Encoding","gzip,deflate,sdch")
req.Header("Host","beego.wiki")
req.Header("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36")
Upload file
PostFile function the first params is the name of form, the second param is the filename or filepath you want to send.
b:=httplib.Post("http://beego.wiki/")
b.Param("username","astaxie")
b.Param("password","123456")
b.PostFile("uploadfile1", "httplib.pdf")
b.PostFile("uploadfile2", "httplib.txt")
str, err := b.String()
if err != nil {
t.Fatal(err)
}
Get Response
Above settings are before sending request, how can we get response after request? Here are the ways:
Method | Type | Description |
---|---|---|
req.Response() |
(*http.Response, error) |
This is a http.Response object. You can get data from it. |
req.Bytes() |
([]byte, error) |
Return raw response body. |
req.String() |
(string, error) |
Return raw response body. |
req.ToFile(filename string) |
error |
Save response body into a file. |
req.ToJson(result interface{}) |
error |
Parse JSON response into the result object. |
req.ToXml(result interface{}) |
error |
Parse XML response into the result object. |