go http 处理gzip响应流

go http 处理gzip响应流

go http 处理 gzip 响应流

解决

import "compress/gzip"	
	resp, err := utils.HttpClient.Do(req)
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		return "", errors.New(strconv.Itoa(resp.StatusCode))
	}
	var reader io.ReadCloser
	switch resp.Header.Get("Content-Encoding") {
	case "gzip":
        //处理gzip响应流
		reader, _ = gzip.NewReader(resp.Body)
		defer reader.Close()
	default:
		reader = resp.Body
	}
	respBytes, err := ioutil.ReadAll(reader)
	if err != nil {
		return "", err
	}
	return string(respBytes), err