How to control gzip compression when sending http request

3,091 views
Skip to first unread message

Harry

unread,
Nov 1, 2015, 11:42:51 PM11/1/15
to golang-nuts
Hello every one,

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. 
Just In case, I tried to set both of value true and false to DisableCompression.
But it could't work well so far.

Now I'm using version1.5 of golang.


My part of code sample is as below.

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

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)


Thanks in advance.


Harry

Giulio Iotti

unread,
Nov 2, 2015, 4:07:58 AM11/2/15
to golang-nuts
On Monday, November 2, 2015 at 6:42:51 AM UTC+2, Harry wrote:
tr := &http.Transport{
DisableCompression: true,
}
client := &http.Client{Transport: tr}

This works as expected, won't send the Accept-Encoding header at all.
 
req.Header.Del("Accept-Encoding")

This is not necessary. If the server ignores the hint of a missing Accept-Encoding: gzip and sends back gzip anyway, try setting req.Header.Set("Accept-Encoding", "identity") (and keep DisableCompression: true in transport)

How did you verify that the header is sent? Notice that if httputil.DumpRequestOut ignores your transport, and thus will have compression enabled again.

-- 
Giulio Iotti

hiroki...@dac.co.jp

unread,
Nov 3, 2015, 8:38:02 PM11/3/15
to golang-nuts
Hello Giulio,

Thank you for your valuable advise.

I vrified http header using httputil.DumpRequestOut.
As your advise, It could work well after using req.Header.Set("Accept-Encoding", "identity").

Thanks again!!




2015年11月2日月曜日 18時07分58秒 UTC+9 Giulio Iotti:
Reply all
Reply to author
Forward
0 new messages