Search code examples
goproxyhttp-proxy

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


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?


Solution

  • 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.