使用的库 go-resty/resty
代码如下
func httpGet(debug bool, url string, queryParam map[string]string) (*resty.Response, error) {
client := resty.New()
client.OnBeforeRequest(func(c *resty.Client, req *resty.Request) error {
var sign string = strconv.Itoa(time.Now().Nanosecond()) + queryParam["account_id"]
req.QueryParam.Set("nonce", util.MD5(sign))
return nil // if its success otherwise return error
})
client.SetDebug(debug)
client.SetTimeout(20 * time.Second)
client.SetAllowGetMethodPayload(true)
client.
SetRetryCount(3).
SetRetryWaitTime(5 * time.Second).
SetRetryMaxWaitTime(10 * time.Second)
client.AddRetryCondition(
func(r *resty.Response, rr error) bool {
if r.StatusCode() != http.StatusOK {
return true
}
result := &MaterialReportResponse{}
json.Unmarshal(r.Body(), result)
if result.Code == 10000 || result.Code == 12100 {
return true
}
return false
},
)
resp, err := client.R().
SetHeaders(map[string]string{"Content-Type": "application/json"}).
SetQueryParams(queryParam).
Get(url)
if err != nil {
util.SugarLogger.Errorf("Request error: url:%s, error:%s", url, err)
}
return resp, err
}