grequests----golang的requests庫


github.com/levigross/grequests:

A Go "clone" of the great and famous Requests library

特點:

  1. 響應可以序列化為JSON和XML
  2. 輕松上傳文件
  3. 輕松下載文件
  4. 支持以下HTTP請求方式:GET,HEAD,POST,PUT,DELETE,PATCH,OPTIONS 

安裝:

go get -u github.com/levigross/grequests

導入:

import "github.com/levigross/grequests"

GET請求:

func req() {
    res, err := grequests.Get("http://www.baidu.com", nil)
    if err != nil{
        log.Fatalln("Unable to make request: ", err)
    }
    fmt.Println(res) // 返回Response, 包括了RawResponse,StatusCode,Header...等等
        //源碼部分
        //func buildResponse(resp *http.Response, err error) (*Response, error) {
        //    // If the connection didn't succeed we just return a blank response
        //    if err != nil {
        //        return &Response{Error: err}, err
        //    }
        //
        //    goodResp := &Response{
        //        // If your code is within the 2xx range – the response is considered `Ok`
        //        Ok:                 resp.StatusCode >= 200 && resp.StatusCode < 300,
        //        Error:              nil,
        //        RawResponse:        resp,
        //        StatusCode:         resp.StatusCode,
        //        Header:             resp.Header,
        //        internalByteBuffer: bytes.NewBuffer([]byte{}),
        //    }
        //    // EnsureResponseFinalized(goodResp) This will come back in 1.0
        //    return goodResp, nil
    fmt.Println(res.StatusCode)
    fmt.Println(res.Error) //Response如果沒有錯誤,Error為nil
    fmt.Println(res.Header) //
    fmt.Println(res.Ok) // 返回bool值,用於驗證返回的狀態碼是否是 2xx系列
    fmt.Println(res.RawResponse) // 返回http.Response,包括了很多東西,可以看源碼
        //源碼部分
        //type Response struct {
        //    Status     string // e.g. "200 OK"
        //    StatusCode int    // e.g. 200
        //    Proto      string // e.g. "HTTP/1.0"
        //    ProtoMajor int    // e.g. 1
        //    ProtoMinor int    // e.g. 0
        //    Header Header
        //    Body io.ReadCloser
        //    ContentLength int64
        //    TransferEncoding []string
        //    Close bool
        //    Uncompressed bool.
        //    Trailer Header
        //    Request *Request
        //    TLS *tls.ConnectionState
        //}
    fmt.Println(res.DownloadToFile("baidu.html"))  // DownloadToFile允許您將響應的內容下載到文件中
    fmt.Println(res.JSON()) // JSON是一種方法,它將使用響應主體中返回的JSON填充“userStruct”提供的結構(解析數據)
    fmt.Println(res.XML()) //XML是一種方法,它將使用響應主體中返回的XML填充“userStruct”提供的結構,
    fmt.Println(res.Bytes()) // 字節以字節數組的方式返回
    res.ClearInternalBuffer //ClearInternalBuffer是一個清除我們用來保存.String()和.Bytes()數據的內部緩沖區的函數。一旦使用了這些功能 - 您可能想要釋放內存。

    n, err := res.Read()  //如果有人想要使用原始數據,那么read是我們支持io.ReadCloser的能力的一部分
    res.Close() //如果有人想要使用原始數據,關閉是我們支持io.ReadCloser的能力的一部分
}

其他可以看文檔:https://godoc.org/github.com/levigross/grequests

github地址:https://github.com/levigross/grequests

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM