go http响应乱码

golang请求接口返回的数据乱码,原因之一是请求头设置了"Accept-Encoding": "gzip, deflate, br",那么如果服务器支持的话响应的数据就会经过gzipdeflatebr等方式的压缩,解决方式是对数据解压,或者可以不设置接收方式,即"Accept-Encoding": ""

import "github.com/andybalholm/brotli"
import "compress/flate"
import "compress/gzip"
// 检测返回的body是否经过压缩,并返回解压的内容
func contentDecoding(res *http.Response) (bodyReader io.Reader, err error) {
	switch res.Header.Get("Content-Encoding") {
	case "gzip":
		bodyReader, err = gzip.NewReader(res.Body)
	case "deflate":
		bodyReader = flate.NewReader(res.Body)
	case "br":
		bodyReader = brotli.NewReader(res.Body)
	default:
		bodyReader = res.Body
	}
	return
}