douqiju2520
douqiju2520
2015-11-02 02:14
浏览 540
已采纳

(转到)发送HTTP请求时如何控制gzip压缩?

I'd like to ask all of you how to control gzip compression when requesting HTTP Post messages. "Accept-Encoding: gzip" as Http request headers was always added to http request I sent. But I don't want to use gzip compression. How can I manage that?

I've always used DisableCompression of transport type before executing http.NewRequest. And I already tried to set both of value true and false to DisableCompression. However it can't work well so far.

My part of code sample is as below.

//gzip
tr := &http.Transport{
    DisableCompression: true,
}
//client := &http.Client{}
client := &http.Client{Transport: tr}

req, err := http.NewRequest(
    "POST",
    reqUrl,
    bytes.NewBuffer(bytesMessage),
)

//Set Http Headers
req.Header.Add("Content-Type", "application/json; charset=UTF-8")
req.Header.Add("Accept", "*/*")
req.Header.Del("Accept-Encoding")

//HTTP request
resp, err := client.Do(req)

Go version I'm using is 1.5.

Thanks in advance.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dro80463
    dro80463 2015-11-02 02:31
    已采纳

    Try

    req.Header.Set("Accept-Encoding", "identity")
    
    点赞 评论

相关推荐