Golang : read gzipped http response
There are times when we want to grab a website content for parsing(crawling) and found out that the content is gzipped.
Normally, to deal with gzipped HTML reply, you can use Exec package to execute curl
from command line and pipe the gzipped content to gunzip, such as this :
curl -H "Accept-Encoding: gzip" http://www.thestar.com.my | gunzip
Another way to process gzipped http response can be done in Golang as well. The following codes will demonstrate how to get same result as the curl
command via Golang.
package main
import (
"compress/gzip"
"fmt"
"io"
"net/http"
"os"
)
func main() {
client := new(http.Client)
request, err := http.NewRequest("Get", " http://www.thestar.com.my", nil)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
request.Header.Add("Accept-Encoding", "gzip")
response, err := client.Do(request)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer response.Body.Close()
// Check that the server actual sent compressed data
var reader io.ReadCloser
switch response.Header.Get("Content-Encoding") {
case "gzip":
reader, err = gzip.NewReader(response.Body)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer reader.Close()
default:
reader = response.Body
}
// to standard output
_, err = io.Copy(os.Stdout, reader)
// see https://www.socketloop.com/tutorials/golang-saving-and-reading-file-with-gob
// on how to save to file
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
References :
http://golang.org/pkg/os/exec/
http://golang.org/pkg/net/http/#Get
https://www.socketloop.com/tutorials/how-to-check-with-curl-if-my-website-or-the-asset-is-gzipped
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+10.3k Golang : Resolve domain name to IP4 and IP6 addresses.
+27.4k PHP : Count number of JSON items/objects
+7k Golang : Check if one string(rune) is permutation of another string(rune)
+7.6k Golang : Generate human readable password
+4.8k Golang : Constant and variable names in native language
+16.6k Golang : Set up source IP address before making HTTP request
+5.1k Unix/Linux : How to archive and compress entire directory ?
+5.7k Golang : Shuffle array of list
+8.6k Golang : HTTP Routing with Goji example
+15.3k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+4.3k Javascript : Detect when console is activated and do something about it
+5.9k Golang : Build new URL for named or registered route with Gorilla webtoolkit example