Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

http

gzip

go

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.

like image 797
Harry Avatar asked Nov 02 '15 02:11

Harry


1 Answers

Try

req.Header.Set("Accept-Encoding", "identity")
like image 92
Rhythmic Fistman Avatar answered Oct 15 '22 19:10

Rhythmic Fistman