強大和著名Requests庫的一個Go克隆:GRequests

jopen 9年前發布 | 38K 次閱讀 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
  • </ul>

    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)
    }

    項目主頁:http://www.baiduhome.net/lib/view/home/1435497384310

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!