Standard library / net/http/httputil

httputil.DumpRequestOut()

Inspect an outgoing request including headers added by the default transport, without performing a real outbound request. It is not an exact capture for arbitrary custom transports. Do not dump live credentials or unbounded bodies. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func DumpRequestOut(req *http.Request, body bool) ([]byte, error)

Parameters & Attributes

NameTypeDescription
req*http.RequestHTTP request to inspect or exchange. Use only synthetic data in dumps.
bodyboolWhether to include the body; true can consume and replace the body in memory.

Return Value

([]byte, error)

Approximate outgoing HTTP/1.x request bytes, including transport-added headers, or error.

Working Example

Example Snippetgo
package main
import (  "bytes"  "fmt"  "net/http"  "net/http/httputil")
func main() {  req := must(http.NewRequest("GET", "http://demo.invalid/a", nil))  b := must(httputil.DumpRequestOut(req, false))  fmt.Println(bytes.Contains(b, []byte("User-Agent: Go-http-client/1.1")))}
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