Standard library / net/http/httputil
httputil.DumpRequest()
Produce an approximate HTTP/1.x representation of an inbound request. Header case/order and HTTP/2 framing are not preserved. With body=true the body is read and replaced; use bounded synthetic inputs and redact secrets before logging. Expected stdout: 'true\nhello\n'.
Formal Syntax & Structure
go syntaxgo
func DumpRequest(req *http.Request, body bool) ([]byte, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| req | *http.Request | HTTP request to inspect or exchange. Use only synthetic data in dumps. |
| body | bool | Whether to include the body; true can consume and replace the body in memory. |
Return Value
([]byte, error)
Approximate HTTP/1.x request bytes or error.
Working Example
Example Snippetgo
package main
import ( "bytes" "fmt" "io" "net/http/httptest" "net/http/httputil" "strings")
func main() { req := httptest.NewRequest("POST", "http://demo.invalid/a", strings.NewReader("hello")) b := must(httputil.DumpRequest(req, true)) fmt.Println(bytes.Contains(b, []byte("hello"))) fmt.Println(string(must(io.ReadAll(req.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) }}