Last active
May 23, 2017 14:30
-
-
Save TheHackerDev/f1dd87ebb894f618b45a68274e7b731c to your computer and use it in GitHub Desktop.
A helper function to read the content from an `http.Response` body and refill the body with another `io.ReadCloser`, so that it can be read again.
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
import ( | |
"bytes" | |
"io/ioutil" | |
"net/http" | |
) | |
// Function readResponseBody is a helper function to read the content from a response's body, | |
// and refill the body with another io.ReadCloser, so that it can be read again. | |
func readResponseBody(resp *http.Response) (content []byte, err error) { | |
// Get the content | |
content, err = ioutil.ReadAll(resp.Body) | |
// Reset the response body | |
rCloser := ioutil.NopCloser(bytes.NewBuffer(content)) | |
resp.Body = rCloser | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you don't refill after reading, the body will return null.