title: How to log the response body from middleware?
body: <p>I tried to log the response body from middleware. I can get all the data related to the Request object. Now try to log the response code and response body for a single request.</p>
  
  <p>I was trying to catch the response body and response code from <code>w http.ResponseWriter</code>, but could not succeed. Here is the sample code. I am using go's default <code>http</code> and <code>logrus</code> for logging.</p>
  
  <pre><code>next.ServeHTTP(w, r.WithContext(ctx))
  end := time.Now().UTC()
  latency := end.Sub(start)
  // request logging
  log.WithFields(log.Fields{
      "path":      r.URL.Path,
      "method":    r.Method,
      "body":      body,
      "auth_user": claims,
      "latency":   latency,
      "useragent": r.Header.Get("User-Agent"),
      "response_body": ,// cannot get response body here
      "status": "", //cannot get response header here
  }).Info("request data")
  </code></pre>
  
  <p>Can someone help me with that, please?</p>