Skip to content

Instantly share code, notes, and snippets.

@TheHackerDev
Last active May 23, 2017 14:30
Show Gist options
  • Save TheHackerDev/f1dd87ebb894f618b45a68274e7b731c to your computer and use it in GitHub Desktop.
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.
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
}
@TheHackerDev
Copy link
Author

If you don't refill after reading, the body will return null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment