Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BadgerBadgerBadgerBadger/402deb43cf80798aae02bd4d277a9619 to your computer and use it in GitHub Desktop.
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.
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