2

I'm trying to proxy a request through Go's net/http package. I copy the http.Request object, send that, and a receive an http.Response object. I want to copy everything from that http.Response object through the http.ResponseWriter back to the client. Something like this, at a glance, would seem to make sense.

http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
    // Copy req into proxyReq
    ...
    proxyRes, err := http.DefaultClient.Do(proxyReq)
    proxyResBody := ioutil.ReadAll(proxyRes.Body)

    for header, value := range proxyRes.Header {
        w.Header().Set(header, value) 
    }
    w.WriteHeader(proxyRes.StatusCode)
    w.Write(proxyResBody)
    ...

}

However, due to the parsing of the http.Response object, things like the content length header seem to not show up in the proxyRes.Header object. This also neglects possible trailers, and other parsed aspects of the http.Response. I was wondering if there is a simpler/more elegant way to pipe the full (including all headers, cookies, trailers, status code, etc.) http.Response object through the http.ResponseWriter to return everything back to the client, whilst making sure no information is missed. Thanks for the help!

4343
  • 39
  • 1

0 Answers0