Standard library / net/http/httputil

httputil.DumpResponse()

Serialize a response for diagnostics. Including a body consumes and replaces it; use small synthetic data and never log sensitive responses indiscriminately. Expected stdout: 'true\nhello\n'.

Formal Syntax & Structure

go syntaxgo
func DumpResponse(resp *http.Response, body bool) ([]byte, error)

Parameters & Attributes

NameTypeDescription
resp*http.ResponseHTTP response to serialize or write.
bodyboolWhether to include the body; true can consume and replace the body in memory.

Return Value

([]byte, error)

Serialized response bytes or error.

Working Example

Example Snippetgo
package main
import (  "bytes"  "fmt"  "io"  "net/http/httptest"  "net/http/httputil")
func main() {  r := httptest.NewRecorder()  fmt.Fprint(r, "hello")  res := r.Result()  defer res.Body.Close()  b := must(httputil.DumpResponse(res, true))  fmt.Println(bytes.Contains(b, []byte("hello")))  fmt.Println(string(must(io.ReadAll(res.Body))))}
func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference