Golang http.Response gzip编写器错误\u内容\u长度\u不匹配
我正在尝试从httputil.ReverseProxy->ModifyResponse gzip代理响应。 所以我只能访问http.Response对象Golang http.Response gzip编写器错误\u内容\u长度\u不匹配,go,gzip,reverse-proxy,Go,Gzip,Reverse Proxy,我正在尝试从httputil.ReverseProxy->ModifyResponse gzip代理响应。 所以我只能访问http.Response对象 res.Body = ioutil.NopCloser(bytes.NewReader(minified)) res.ContentLength = int64(len(minified)) res.Header.Set("Content-Length", strconv.Itoa(len(minified))) res.Header.Del(
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")
这个很好用。但是当我gzip内容时,我会得到一个内容长度不匹配的错误
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")
谁知道我做错了什么?即使输入更改,内容长度也始终为10。您没有关闭
gz
编写器。这是一个可能的问题gzip。Writer
说:
完成后,调用方负责调用WriteCloser上的Close。写入操作可能会被缓冲,直到关闭后才会刷新
因此,在完成数据写入后,尝试添加
gz.Close()
。您没有关闭gz
编写器。这是一个可能的问题gzip。Writer
说:
完成后,调用方负责调用WriteCloser上的Close。写入操作可能会被缓冲,直到关闭后才会刷新
因此,在完成数据写入后,尝试添加gz.Close()