Created
August 15, 2018 12:52
-
-
Save kladov/fcb67a700d03efa52a83b205c8c4b9f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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