多次从阅读器读取

I'm building a simple caching proxy that intercepts HTTP requests, grabs the content in response.Body, then writes it back to the client. The problem is, as soon as I read from response.Body, the write back to the client contains an empty body (everything else, like the headers, are 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 remove the content, _ and cachePage lines, it works fine. With the lines included, requests return and empty body. Any idea how I can get just the Body of the http.Response and still write out the response in full to the http.ResponseWriter?

You do not need to read from the response a second time. You already have the data in hand and can write it directly to the response writer.

The call

response.Write(w)

writes the response in wire format to the server's response body. This is not what you want for a proxy. You need to copy the headers, status and body to the server response individually.

I have noted other issues in the code comments below.

I recommend using the standard library's ReverseProxy or copying it and modifying it to meet your needs.

func requestHandler(w http.ResponseWriter, r *http.Request) {

    // No need to make a client, use the default
    // client := &http.Client{} 

    r.RequestURI = ""
    response, err := http.DefaultClient.Do(r)

    // response can be nil, close after error check
    // defer response.Body.Close() 

    if err != nil {
        log.Fatal(err)
    }
    defer response.Body.Close() 

    // Check errors! Always.
    // content, _ := ioutil.ReadAll(response.Body)
    content, err := ioutil.ReadAll(response.Body)
    if err != nil {
         // handle error
    }
    cachePage(response.Request.URL.String(), content)

    // The Write method writes the response in wire format to w.
    // Because the server handles the wire format, you need to do
    // copy the individual pieces.
    // response.Write(w)

    // Copy headers
    for k, v := range response.Header {
       w.Header()[k] = v
    }
    // Copy status code
    w.WriteHeader(response.StatusCode)

    // Write the response body.
    w.Write(content)
}

As you have discovered, you can only read once from a request's Body.

Go has a reverse proxy that will facilitate what you are trying to do. Check out httputil.ReverseProxy and httputil.DumpResponse

As in my comment you could implement the io.ReadCloser

As per Dewy Broto (Thanks) you can do this much simpler with:

content, _ := ioutil.ReadAll(response.Body)
response.Body = ioutil.NopCloser(bytes.NewReader(content))
response.Write(w)