Harry
Harry

Reputation: 1337

(Go) How to control gzip compression when sending http request?

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.

Upvotes: 7

Views: 4765

Answers (1)

Gordon Childs
Gordon Childs

Reputation: 36074

Try

req.Header.Set("Accept-Encoding", "identity")

Upvotes: 13

Related Questions