Created
January 8, 2019 07:37
-
-
Save ik5/06d80e68998ae06c7f078a9cd60fbf77 to your computer and use it in GitHub Desktop.
How to reuse stream in golang
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
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