Gorequest指南
gorequest为 golang 程序提供了极为简便的方式发起 HTTP 请求。网上关于这个库的中文教程不多,因此把官方的 README 文件翻译过来,结合自己的一些使用经验,希望能为各位 Gopher 提供一些帮助。
特性
- 支持发送Get/Post/Put/Head/Delete/Patch/Options 请求
- 建议的请求头设置
- JSON 支持:以 JSON 格式字符串作为函数参数的方式简化传输 JSON 的步骤。
- 分段支持:分段请求(Multipart Request)的方式发送数据或传输文件
- 代理:支援通过代理的方式发送请求。
- Timeout:为请求设置时间限制
- TLS(传输层安全协议)相关设定。
TLSClientConfig - taking control over tls where at least you can disable security check for https
- 重定向策略
- Cookie:为请求添加 cookie
- CookieJar - automatic in-memory cookiejar
- 基本的权限认证。
安装
1 | go get github.com/parnurzeal/gorequest |
文档
参阅Go Doc
后续我会根据自己在开发中使用经验将文档翻译过来。
使用 GoRequest 的一万个理由?
通过 GoRequest 可以使工作变得更简单,使可以发起 HTTP 请求这件事更加优雅而充满乐趣。
不使用本库发起简单 GET 请求:
1 | resp, err := http.Get("http://example.com/") |
使用 GoRequest
1 | request := gorequest.New() |
如果你不想重用request
,也可以写成下面这样
1 | resp, body, errs := gorequest.New().Get("http://example.com/").End() |
如果你需要设定 HTTP 头,设定重定向策略等,使用标准库会瞬间使事情变得异常复杂,在发起仅仅一个 GET 请求的过程,你就需要一个 Client
,通过一系列不同的命令来设定 HTTP 头(HTTP Headers
)。
1 | client := &http.Client{ |
现在,你有更加美妙的方式来完成这件事
1 | request := gorequest.New() |
发起 DELETE, HEAD, POST, PUT, PATCH 请求的过程和发起 GET 请求类似。
1 | request := gorequest.New() |
处理 JSON
用标准库发起 JSON POST ,你需要先将 map
或者 struct
格式的数据包装(Marshal)成 JSON 格式的数据,将头参数设定为’application/json’(必要时还要设定其他头),然后要新建一个http.CLient
变量。经过这一系列的步骤,你的代码变得冗长而难以维护
1 | m := map[string]interface{}{ |
至于 GoRequest, JSON 支持是必须的,所以,用这个库你只需要一行代码完成所有工作
1 | request := gorequest.New() |
另外,它同样支持结构体类型。所以,你可以在你的请求中发送不同的数据类型(So, you can have a fun Mix & Match sending the different data types for your request)。
1 | type BrowserVersionSupport struct { |
Not only for Send()
but Query()
is also supported. Just give it a try! :)
回调(Callback)
此外,GoRequest 支持回调函数,这让你可以更加灵活的使用这个库。
下面是回调函数的一个例子
1 | func printStatus(resp gorequest.Response, body string, errs []error){ |
Multipart/Form-Data
你可以将请求的内容类型设定为multipart
来以multipart/form-data
的方式发送所有数据。这个特性可以帮助你发送多个文件。
下面是一个例子
1 | gorequest.New().Post("http://example.com/"). |
如果感兴趣可以在文档中查看SendFile
函数部分获取更多。
代理(Proxy)
需要使用代理的时候,可以用 GoRequest Proxy Func 很好的处理。
1 | request := gorequest.New().Proxy("http://proxy:999") |
基本认证
添加基本的认证头信息
1 | request := gorequest.New().SetBasicAuth("username", "password") |
超时处理(Timeout)
与 time
库结合可以设置成任何的时间限制。
1 | request := gorequest.New().Timeout(2*time.Millisecond) |
Timeout
函数同时设定了连接和 IO 的时间限制。
以字节方式处理返回体(EndBytes)
1 | resp, bodyBytes, errs := gorequest.New().Get("http://example.com/").EndBytes() |
以结构体的方式处理返回体
假设 URL http://example.com/ 的返回体{"hey":"you"}
。
1 | heyYou struct { |
连续重复请求(Retry)
假设你在得到 BadRequest 或服务器内部错误(InternalServerError)时进行连续三次,间隔五秒的连接尝试
1 | request := gorequest.New() |
重定向
Redirects can be handled with RedirectPolicy which behaves similarly to net/http Client’s CheckRedirect function. Simply specify a function which takes the Request about to be made and a slice of previous Requests in order of oldest first. When this function returns an error, the Request is not made.
1 | request := gorequest.New() |
Debug 模式
For debugging, GoRequest leverages
httputil
to dump details of every request/response. (Thanks to @dafang).You can just useSetDebug
or environment variableGOREQUEST_DEBUG=0|1
to enable/disable debug mode andSetLogger
to set your own choice of logger.Thanks to @QuentinPerez, we can see even how gorequest is compared to CURL by usingSetCurlCommand
.
注意
gorequest.New()
函数应该一次调用,对返回的实例尽可能多次使用。
Credits
- Renee French - the creator of Gopher mascot
- Wisi Mongkhonsrisawat for providing an awesome GoRequest’s Gopher image :)
License
GoRequest is MIT License.