title: Reading gzipped HTTP response in Go
body: <p>I am trying to read a gzipped HTTP response with Go! but I always get the following error message :</p>
  
  <pre><code>panic: gzip: invalid header
  [...] stack trace [...]
  </code></pre>
  
  <p>If I run "curl -H "Accept-Encoding: gzip" <a href="http://foo.com/" rel="noreferrer">http://foo.com/</a> | gunzip -" I get the response correctly gunzipped. I also double checked with ngrep and the pair Accept-Encoding/Content-Encoding is correctly sent/returned.</p>
  
  <p>If I create a file with some dummy content and gzip it, I can read it from my Go! program.</p>
  
  <p>The program I used for testing:</p>
  
  <pre><code>package main
  
  import (
      "io"
      //"os"
      "fmt"
      "compress/gzip"
      "net/http"
  )
  
  func main() {
      /* This works fine
      f, _ := os.Open("/tmp/test.gz")
      defer f.Close()
      reader, err := gzip.NewReader(f)
      */
  
      // This does not :/
      resp, _ := http.Get("http://foo.com/")
      defer resp.Body.Close()
      reader, err := gzip.NewReader(resp.Body)
  
      if err != nil { panic(err) }
  
      buff := make([]byte, 1024)
      for {
          n, err := reader.Read(buff)
  
          if err != nil &amp;&amp; err != io.EOF {
              panic(err)
          }
  
          if n == 0 {
              break
          }
      }
  
      s := fmt.Sprintf("%s", buff)
      fmt.Println(s)
  }
  </code></pre>
  
  <p>Have I overlooked something ?</p>