我想在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