Skip to content

Instantly share code, notes, and snippets.

@kladov
Created August 15, 2018 12:52
Show Gist options
  • Save kladov/fcb67a700d03efa52a83b205c8c4b9f4 to your computer and use it in GitHub Desktop.
Save kladov/fcb67a700d03efa52a83b205c8c4b9f4 to your computer and use it in GitHub Desktop.
// formatRequest generates ascii representation of a request
func formatRequest(r *http.Request) string {
// Create return string
var request []string
// Add the request string
url := fmt.Sprintf("%v %v %v", r.Method, r.URL, r.Proto)
request = append(request, url)
// Add the host
request = append(request, fmt.Sprintf("Host: %v", r.Host))
// Loop through headers
for name, headers := range r.Header {
name = strings.ToLower(name)
for _, h := range headers {
request = append(request, fmt.Sprintf("%v: %v", name, h))
}
}
// If this is a POST, add post data
if r.Method == "POST" {
r.ParseForm()
request = append(request, "\n")
request = append(request, r.Form.Encode())
}
// Return the request as a string
return strings.Join(request, "\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment