Debug HTTP server in Go

Content might be outdated

Sometimes it is useful to see what some client (browser, script) is sending to the webserver. If you don't have access to the server to debug, send the traffic to your own "debug server"

 

 1package main
 2
 3import (
 4	"fmt"
 5	"net/http"
 6	"net/http/httputil"
 7)
 8
 9func main() {
10	http.HandleFunc("/", printDebug)
11	http.ListenAndServe(":8080", nil)
12}
13
14func printDebug(w http.ResponseWriter, r *http.Request) {
15	// Save a copy of this request for debugging.
16	requestDump, err := httputil.DumpRequest(r, true)
17	if err != nil {
18		fmt.Println(err)
19	}
20	fmt.Println(string(requestDump))
21}

Ref comment from Lars Grote to a Medium post https://medium.com/doing-things-right/pretty-printing-http-requests-in-golang-a918d5aaa000