douzhuanfen5923
2018-06-04 01:45
浏览 2.7k
已采纳

在Go中执行HTTP请求时出现乱码

I met a Garbled string question when do Get request in Go, the code is:

req , err:= http.NewRequest(httpMethod, url,strings.NewReader(""))
req.Header.Add("Accept","application/json")
resp, err := http.DefaultClient.Do(req)
body,err := ioutil.ReadAll(resp.Body)
ret := string(body)
log.Warningf("ret: %+v", ret)

if the ret contains only english, it's correct, if contains Chinese, it has garbled string, how to solve this problem, thanks all!

图片转代码服务由CSDN问答提供 功能建议

在Go中执行Get请求时遇到了乱码问题,代码为:</ p> \ n

  req,错误:= http.NewRequest(httpMethod,url,strings.NewReader(“”))
req.Header.Add(“ Accept”,“ application / json”)
resp,err:  = http.DefaultClient.Do(req)
body,err:= ioutil.ReadAll(resp.Body)
ret:= string(body)
log.Warningf(“ ret:%+ v”,ret)
 </ 代码> </ pre> 
 
 

如果ret只包含英语,那是正确的;如果包含中文,则字符串有乱码,如何解决此问题,谢谢!</ p> </ div >

2条回答 默认 最新

相关推荐 更多相似问题