(Go) How to control gzip compression when sending

2020-07-14 03:27发布

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.

标签: http go gzip
1条回答
叼着烟拽天下
2楼-- · 2020-07-14 03:44

Try

req.Header.Set("Accept-Encoding", "identity")
查看更多
登录 后发表回答