fasthttp client example

2018-11-20  本文已影响0人  pandanrain

说明

官方文档关于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
}

上一篇下一篇

猜你喜欢

热点阅读