美文网首页
fasthttp client example

fasthttp client example

作者: pandanrain | 来源:发表于2018-11-20 17:49 被阅读0次

    说明

    官方文档关于fasthttp的demo基本都是http server, http client的介绍基本没有, 这里只写个client的示例。
    参考源: http://big-elephants.com/2016-12/fasthttp-client/

      client := fasthttp.Client{TLSConfig: &tls.Config{InsecureSkipVerify: true}}
      req := fasthttp.AcquireRequest()
      req.SetRequestURI("url")
      req.Header.SetMethod("POST")
      req.Header.Set("Content-Type", "application/json")
      resp := fasthttp.AcquireResponse()    
      req.SetBodyString(`{"body": "json_str"}`)  //设置请求参数
      req.SetBody([]byte(`{"body": "sjon_str"}`)) //设置[]byte
      if err := client.Do(req, resp); err != nil {
            fmt.Printf("loan list fail to do request. appID=%s. [err=%v]\n", h["X-PPD-APPID"], err)
            continue
      }
      b := resp.Body()
      if resp.StatusCode() != fasthttp.StatusOK {
          fmt.Printf("loan list failed code=%d. [err=%v]\n", resp.StatusCode(), string(b))
          continue
    }
    
    

    相关文章

      网友评论

          本文标题:fasthttp client example

          本文链接:https://www.haomeiwen.com/subject/pafgfqtx.html