19

I don't know if this question makes any sense, but I was wondering If there is any way to get the data which is written in http.ResponseWriter. I need it for logging.

I have written an API in Go.

func api1(w http.ResponseWriter, req *http.Request) {       
    var requestData MyStruct
    err := json.NewDecoder(req.Body).Decode(&requestData)
    if err != nil {
        writeError(w, "JSON request is not in correct format")
        return
    }
    log.Println(" Request Data :", req.Body) // I am logging req
    result, err := compute()                 // getting result from a function

    if err != nil {
        errRes := ErrorResponse{"ERROR", err}
        response, er = json.Marshal(errRes) // getting error response
    } else {
        response, er = json.Marshal(result)
    }
    if er != nil {
        http.Error(w, er.Error(), 500) // writing error
        return
    }
    io.WriteString(w, string(response)) // writing response
}

The aim is to create a single log with request and response data. The response can be either an error response or processed response.

I was thinking if I could get data which is written on http.ResponseWriter then, I can create single meaningful log.

Is this possible? If not, please suggest how can I achieve this.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Jagrati
  • 11,474
  • 9
  • 35
  • 56
  • 1
    The `http.ResponseWriter` passed to handlers does not support this, but it's easy to wrap it to support your needs. For an example, check out this possible duplicate (it details wrapping both the `Request` and the `ResponseWriter`): [Golang read request body](http://stackoverflow.com/questions/43021058/golang-read-request-body/43021236#43021236) – icza May 05 '17 at 09:58
  • icza's should be the accepted answer answer. MultiWriter generates a Writer not a ResponseWriter.. you need to add to that to make it interchangeable in handlers – Riccardo Manfrin Oct 26 '20 at 15:39

2 Answers2

14

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.

ain
  • 22,394
  • 3
  • 54
  • 74
2

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
    }
    ...
}
openwonk
  • 14,023
  • 7
  • 43
  • 39