How to dump a response of an HTTP GET request and write it in http.ResponseWriter
Asked Answered
Q

1

5

I am trying to do it to dump the response of an HTTP GET request and write the very same response in an http.ResponseWriter. Here is my code:

package main

import (
    "net/http"
    "net/http/httputil"
)

func handler(w http.ResponseWriter, r *http.Request) {
    resp, _ := http.Get("http://google.com")
    dump, _ := httputil.DumpResponse(resp,true)
    w.Write(dump)
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

I get a full page of HTML code of google.com instead of the Google front page. Is there a way I can achieve a proxy-like effect?

Quickel answered 24/12, 2016 at 14:6 Comment(1)
You should be writing the response's body to w not the whole response(http headers + body): body, _ := ioutil.ReadAll(resp.Body); w.Write(body).Repression
H
13

Copy the headers, status and response body to the response writer:

resp, err :=http.Get("http://google.com")
if err != nil {
    // handle error
}
defer resp.Body.Close()

// headers

for name, values := range resp.Header {
    w.Header()[name] = values
}

// status (must come after setting headers and before copying body)

w.WriteHeader(resp.StatusCode)

// body

io.Copy(w, resp.Body)

If you are creating a proxy server, then the net/http/httputil ReverseProxy type might be of help.

Herzig answered 24/12, 2016 at 18:25 Comment(1)
I struggled to understand why io.Copy has to be after w.WriteHeader. Looking at the http documentation I found this: "If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK) before writing the data. If the Header does not contain a Content-Type line, Write adds a Content-Type set to the result of passing the initial 512 bytes of written data to DetectContentType. Additionally, if the total size of all written data is under a few KB and there are no Flush calls, the Content-Length header is added automatically." golang.org/pkg/net/httpPique

© 2022 - 2024 — McMap. All rights reserved.