go语言封装http请求工具类(访问外部接口)

代码

	// example: http://host:port/uri/?param1=1&param2=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
}
发布了39 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42123182/article/details/95208835