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?
Copy the headers, status and response body to the response writer:
If you are creating a proxy server, then the net/http/httputil ReverseProxy type might be of help.