Unexpected EOF using Go http client

I am learning Go and have come across this problem.

I just load the contents of a webpage using an HTTP client:

package main import ( "fmt" "io/ioutil" "log" "net/http" ) func main() { client := &http.Client{} req, err := http.NewRequest("GET", "https://mail.ru/", nil) req.Close = true response, err := client.Do(req) if err != nil { log.Fatal(err) } defer response.Body.Close() content, err := ioutil.ReadAll(response.Body) if err != nil { fmt.Println(err) } fmt.Println(string(content)[:100]) } 

When reading the response body, an unexpected EOF error occurs. At the same time, the content variable has the full content of the page.

This error appears only when downloading content https://mail.ru/ . With other urls everything works fine - no errors.

I used curl to load the contents of this page - everything works as expected.

I'm a little confused - what's going on here?

Go v1.2, tried Ubuntu and MacOS X

+7
go error-handling
source share
1 answer

This server (Apache 1.3, wow!) Seems to be serving a truncated gzip response. If you explicitly request an identity encoding (not allowing the Go transport from adding gzip ), you will not get ErrUnexpectedEOF :

 req.Header.Add("Accept-Encoding", "identity") 
+7
source

All Articles