Created
September 3, 2018 12:11
-
-
Save antonu17/404acd209e6cf806713ff9cace85c23e to your computer and use it in GitHub Desktop.
Golang: Read from an io.ReadWriter without losing its content
This file contains hidden or 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
// 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