Skip to content

Instantly share code, notes, and snippets.

@chris-skud
Created January 7, 2018 19:03
Show Gist options
  • Save chris-skud/ea8648305aba795a617d28d86c729232 to your computer and use it in GitHub Desktop.
Save chris-skud/ea8648305aba795a617d28d86c729232 to your computer and use it in GitHub Desktop.
retain value of read closer by copying, reading, re-setting.
// source https://medium.com/@xoen/golang-read-from-an-io-readwriter-without-loosing-its-content-2c6911805361
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