Created
August 23, 2022 09:43
-
-
Save BadgerBadgerBadgerBadger/402deb43cf80798aae02bd4d277a9619 to your computer and use it in GitHub Desktop.
Golang: Read full request body but reset it so downstream handlers can still use it.
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
func(w http.ResponseWriter, r *http.Request) { | |
body, err := io.ReadAll(r.Body) | |
if err != nil { | |
// do some error handling | |
return | |
} | |
r.Body.Close() | |
// since the body has been drained, reconstruct it as a ReadCloser | |
// for further consumption downstream | |
r.Body = io.NopCloser(bytes.NewBuffer(body)) | |
next.ServeHTTP(w, r) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment