Skip to content

Instantly share code, notes, and snippets.

@ik5
Created January 8, 2019 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ik5/06d80e68998ae06c7f078a9cd60fbf77 to your computer and use it in GitHub Desktop.
Save ik5/06d80e68998ae06c7f078a9cd60fbf77 to your computer and use it in GitHub Desktop.
How to reuse stream in golang
package utils
import (
"bytes"
"io"
"io/ioutil"
"net/http"
)
// PeekReadCloser return a copy of a reader without loosing the original content
func PeekReadCloser(stream io.ReadCloser, cpy *[]byte) {
*cpy, _ = ioutil.ReadAll(stream)
stream = ioutil.NopCloser(bytes.NewReader(*cpy))
}
// PeekHTTPRequestBody return a copy of a reader without loosing the original content
func PeekHTTPRequestBody(r *http.Request, cpy *[]byte) {
PeekReadCloser(r.Body, cpy)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment