I'm trying to gzip the proxied response from httputil.ReverseProxy -> ModifyResponse. So I only have access to the http.Response object.
res.Body = ioutil.NopCloser(bytes.NewReader(minified))
res.ContentLength = int64(len(minified))
res.Header.Set("Content-Length", strconv.Itoa(len(minified)))
res.Header.Del("Content-Encoding")
This works just fine. But when I gzip the content I'll get a content-length-mismatch error.
var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
gz.Write(minified)
readCloser := ioutil.NopCloser(&buf)
res.Body = readCloser
res.ContentLength = int64(buf.Len())
res.Header.Set("Content-Length", strconv.Itoa(buf.Len()))
res.Header.Set("Content-Encoding", "gzip")
Can anyone tell what i'm doing wrong? The content-length is always 10 even when the input changes.
You're not closing your gz
writer. It's possible issue. gzip.Writer
documentation says:
It is the caller's responsibility to call Close on the WriteCloser when done. Writes may be buffered and not flushed until Close.
So, try to add gz.Close()
after you've completed writing data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.