Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Golang http.Response gzip编写器错误\u内容\u长度\u不匹配_Go_Gzip_Reverse Proxy - Fatal编程技术网

Golang http.Response gzip编写器错误\u内容\u长度\u不匹配

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(

我正在尝试从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("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()