DumpRequest Wire Level Preview
Preview the wire format of a request with httputil.DumpRequest so the request line, Host header, and body framing are visible without a packet capture.
Code
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"net/http/httputil"
"strings"
)
func main() {
body := strings.NewReader("order=widget qty=2")
// httptest.NewRequest 构造服务端请求:RequestURI、Proto 均已就位
req := httptest.NewRequest(http.MethodPost, "/orders", body)
req.Host = "api.example.com"
req.Header.Set("X-Request-Id", "req-42")
// DumpRequest 还原线路级形态:请求行、头部与正文框架一次看完
dump, err := httputil.DumpRequest(req, true)
if err != nil {
fmt.Println("dump error:", err)
return
}
fmt.Println(string(dump))
}How the snippet works
Everything that executes sits inside the function main.
Error handling is inline: 1 place test err != nil and branch immediately.
Caveats
- Nothing to install: the snippet stays inside the standard library.
Applying it
The signature you will call most reads func main()
Every run emits labeled lines — dump error: — when the file runs.
Statements that matter most here
if err != nil {
fmt.Println("dump error:", err)
return
}
Interactive Tools for This Topic
cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.HAR Waterfall AnalyzerInspect HTTP Archive (HAR 1.2) JSON files locally to summarize total requests, status codes, MIME types, slowest calls, and timing waterfalls.HTTP Header AnalyzerPaste raw HTTP header lines and get a parsed name-value list with plain-language notes for common headers and security observations.Accept Header ParserParse an Accept header into media ranges with their quality values and specificity, sorted by preference for content negotiation debugging.
FAQ
- What does DumpRequest Wire Level Preview print when run?
- No setup needed: run the file and look for labeled lines like `dump error:` in the output.
Report a problem with this exampleOpens a pre-filled issue on GitHub.