Skip to content

Instantly share code, notes, and snippets.

@antonu17
Created September 3, 2018 12:11
Show Gist options
  • Save antonu17/404acd209e6cf806713ff9cace85c23e to your computer and use it in GitHub Desktop.
Save antonu17/404acd209e6cf806713ff9cace85c23e to your computer and use it in GitHub Desktop.
Golang: Read from an io.ReadWriter without losing its content
// Read the content
var bodyBytes []byte
if c.Request.Body != nil {
bodyBytes, _ = ioutil.ReadAll(c.Request.Body)
}
// Restore the io.ReadCloser to its original state
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
// Use the content
bodyString := string(bodyBytes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment