强大和著名Requests库的一个Go克隆:GRequests
jopen
9年前
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) }