How to get data from http.ResponseWriter for logging

Adding to accepted answer.

I love using io.MultiWriter, however it only writes the response body in this context.

If you want the headers, use the .Headers().Write function like this...

func (w http.ResponseWriter, req *http.Request) {
    var log bytes.Buffer
    rsp := io.MultiWriter(w, &log)
    // from this point on use rsp instead of w, ie

    // get the response headers
    w.Header().Write(&log)

    err := json.NewDecoder(req.Body).Decode(&requestData)
    if err != nil {
        writeError(rsp, "JSON request is not in correct format")
        return
    }
    ...
}

You could use io.MultiWriter - it creates a writer that duplicates its writes to all the provided writers. So to log the reponse

func api1(w http.ResponseWriter, req *http.Request) {
    var log bytes.Buffer
    rsp := io.MultiWriter(w, &log)
    // from this point on use rsp instead of w, ie
    err := json.NewDecoder(req.Body).Decode(&requestData)
    if err != nil {
        writeError(rsp, "JSON request is not in correct format")
        return
    }
    ...
}

Now you have duplicate of the info written into rsp in both w and log and you can save the content of the log buffer onto disc of show it on console etc.

You can use io.TeeReader to create a Reader that writes to given Writer what it reads from given reader - this would allow you to save copy of the req.Body into log, ie

func api1(w http.ResponseWriter, req *http.Request) {
    var log bytes.Buffer
    tee := io.TeeReader(req.Body, &log)
    err := json.NewDecoder(tee).Decode(&requestData)
    ...
}

Now since json decoder reads form tee the content of the req.Body is also copied into the log buffer.

Tags:

Go