代码
// example: http://host:port/uri/?param1=1¶m2=2
func Get(reqUrl string, jsonParams interface{}, toWho string) ([]byte, error) {
var params url.Values = url.Values{}
var jsonObj map[string]interface{}
jsonB := StringifyJsonToBytes(jsonParams)
//logger.L.Info("["+toWho+"]请求参数:"+string(jsonB), "url", reqUrl)
if err := ParseJsonFromBytes(jsonB, &jsonObj); err != nil {
logger.L.Trace(err)
return nil, err
}
for k, v := range jsonObj {
params.Set(k, fmt.Sprintf("%v", v))
}
logger.L.Debug("["+toWho+"]get请求url:", "url", reqUrl+"?"+params.Encode())
req, rErr := http.NewRequest("GET", reqUrl+"?"+params.Encode(), nil)
if rErr != nil {
return nil, rErr
}
//req.Header.Set("sign", sig)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
logger.L.Error("["+toWho+"]请求不成功:", "err", err.Error())
return nil, err
}
defer resp.Body.Close()
b, err2 := ioutil.ReadAll(resp.Body)
if err2 != nil {
logger.L.Error("["+toWho+"]读取响应体报错:", "err", err2.Error())
return nil, err2
}
if len(string(b)) <= 200 {
logger.L.Info("[" + toWho + "]请求结果:" + string(b))
} else {
logger.L.Info("[" + toWho + "]请求结果太长不打印")
}
if resp.StatusCode != 200 {
return nil, errors.New("[" + toWho + "]请求不成功:" + string(b))
}
return b, nil
}