Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gzip uncompressed http.Response.Body

Tags:

gzip

go

I am building a Go application that takes an http.Response object and saves it (response headers and body) to a redis hash. When the application receives an http.Response.Body that is not gzipped, I want to gzip it before saving it to the cache.

My confusion stems from my inability to make clear sense of Go's io interfaces, and how to negotiate between http.Response.Body's io.ReadCloser and the gzip Writer. I imagine there is an elegant, streaming solution here, but I can't quite get it to work.

like image 230
Squirkle Avatar asked Jun 25 '15 17:06

Squirkle


1 Answers

If you've already determined the body is uncompressed, and if you need a []byte of the compressed data (instead of for example already having an io.Writer you could write to, e.g. if you wanted to save the body to a file then you'd want to stream into the file not into a buffer) then something like this should work:

func getCompressedBody(r *http.Response) ([]byte, error) {
    var buf bytes.Buffer
    gz := gzip.NewWriter(&buf)
    if _, err := io.Copy(gz, r.Body); err != nil {
        return nil, err
    }
    err := gz.Close()
    return buf.Bytes(), err
}

(this is just an example and would probably be in-line instead of as a function; if you wanted it as a fuction then it should probably take an io.Reader instead of an *http.Response).

like image 172
Dave C Avatar answered Oct 16 '22 15:10

Dave C