I create a simple proxy server that intercepts HTTP requests, captures the contents in response.Body, and then writes it back to the client. The problem is that as soon as I read the answer. Body, the record back to the client contains an empty body (everything else, like the headers, is written as expected).
Here's the current code:
func requestHandler(w http.ResponseWriter, r *http.Request) {
client := &http.Client{}
r.RequestURI = ""
response, err := client.Do(r)
defer response.Body.Close()
if err != nil {
log.Fatal(err)
}
content, _ := ioutil.ReadAll(response.Body)
cachePage(response.Request.URL.String(), content)
response.Write(w)
}
If I delete the lines content, _and cachePageit works fine. With strings turned on, queries are returned and the body is empty. Any idea how I can only get Bodyin http.Responseand write a complete answer to http.ResponseWriter?
source
share