package main import ( "html/template" "io" "log" "net/http" ) // RequestData holds all the information we want to display about a request type RequestData struct { Method string URL string Protocol string Scheme string Host string Path string RawQuery string QueryParams map[string][]string Headers map[string][]string Body string HasQuery bool } // collectRequestData extracts all relevant information from the HTTP request func collectRequestData(r *http.Request) (*RequestData, error) { body, err := io.ReadAll(r.Body) if err != nil { return nil, err } defer r.Body.Close() bodyString := string(body) if len(bodyString) == 0 { bodyString = "(empty)" } data := &RequestData{ Method: r.Method, URL: r.URL.String(), Protocol: r.Proto, Scheme: r.URL.Scheme, Host: r.Host, Path: r.URL.Path, RawQuery: r.URL.RawQuery, QueryParams: r.URL.Query(), Headers: r.Header, Body: bodyString, HasQuery: len(r.URL.Query()) > 0, } return data, nil } const echoTemplate = `
Method:{{.Method}}
URL:{{.URL}}
Protocol:{{.Protocol}}
Scheme:{{.Scheme}}
Host:{{.Host}}
Path:{{.Path}}
RawQuery:{{.RawQuery}}
{{$key}}:{{.}}
{{end}} {{end}} {{else}}No query parameters
{{end}}| Header | Value |
|---|---|
| {{$name}} | {{range $i, $v := $values}}{{if $i}}, {{end}}{{$v}}{{end}} |
{{.Body}}