說明
官方文檔關(guān)于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"}`) //設置請求參數(shù)
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
}