我想在golang中编写HTTP代理。我使用这个模块代理:https://github.com/elazarl/goproxy。当有人使用我的代理时,它会以http.Response作为输入来调用一个函数。我们称之为“resp”。 resp.Body是一个io.ReadCloser。我可以通过读取方法将它读入[]字节数组中。但是,它的内容已经从resp.Body中消失了。但是我必须返回一个http.Response,并将其读入一个[]字节的数组。我怎样才能做到这一点?读取缓冲区并将其重写到http.Response中去
问候,
最大
我的代码:
proxy.OnResponse().DoFunc(func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
body := resp.Body
var readBody []byte
nread, readerr := body.Read(readBody)
//the body is now empty
//and i have to return a body
//with the contents i read.
//how can i do that?
//doing return resp gives a Response with an empty body
}
我想这是'NopCloser'与单个o。 – inf
@inf:谢谢:) – JimB