强大和著名Requests库的一个Go克隆:GRequests
GRequests是强大和著名Requests库的一个Go克隆。
Features
- Asynchronous and synchronous functionality built in
- Doesn't depend on external libraries (functionality is designed to complementnet/http)
- Works with every version of Go from 1.3
- Responses can be serialized into JSON and XML
- Easy file uploads
- Easy file downloads
- Support for the following HTTP verbsGET, HEAD, POST, PUT, DELETE, PATCH, OPTIONS
Install
go get -u github.com/levigross/grequests
Usage
import "github.com/levigross/grequests"
Basic Examples
Basic GET request:
resp, err := grequests.Get("http://httpbin.org/get", nil) // You can modify the request by passing an optional RequestOptions struct if err != nil { log.Fatalln("Unable to make request: ", err) } fmt.Println(resp.String()) // { // "args": {}, // "headers": { // "Accept": "*/*", // "Host": "httpbin.org",
We also support asynchronous functions that return aResponsechannel
respChan := grequests.GetAsync("http://httpbin.org/get", nil) select { case resp := <-respChan: fmt.Println(resp.String()) // { // "args": {}, // "headers": { // "Accept": "*/*", // "Host": "httpbin.org", }
When making a asynchronous request, it is very important to check the.Errorproperty of theResponsee.g:
resp := grequests.GetAsync("http://httpbin.org/xml", nil) if resp.Error != nil { log.Fatalln("Unable to make request", resp.Error) }
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!