Created
January 7, 2018 19:03
-
-
Save chris-skud/ea8648305aba795a617d28d86c729232 to your computer and use it in GitHub Desktop.
retain value of read closer by copying, reading, re-setting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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