Last active
April 12, 2018 12:40
-
-
Save rochacon/9341135 to your computer and use it in GitHub Desktop.
Simple HTTP request dumper
This file contains hidden or 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
// dumper is a simple HTTP server that dumps requests to stdout | |
// useful for debugging HTTP clients | |
package main | |
import ( | |
"bytes" | |
"flag" | |
"io" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"net/http/httputil" | |
"os" | |
) | |
func main() { | |
var listen string | |
flag.StringVar(&listen, "l", "", "Address to listen to, e.g. 127.0.0.1:3000.") | |
flag.Parse() | |
if port := os.Getenv("PORT"); port != "" { | |
listen = "0.0.0.0:" + port | |
} | |
if listen == "" { | |
listen = "0.0.0.0:3000" | |
} | |
http.HandleFunc("/", dumper) | |
log.Printf("Listening on: %s\n", listen) | |
err := http.ListenAndServe(listen, nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func dumper(w http.ResponseWriter, r *http.Request) { | |
req, err := httputil.DumpRequest(r, true) | |
if err != nil { | |
req, err = ioutil.ReadAll(r.Body) | |
defer r.Body.Close() | |
if err != nil { | |
http.Error(w, "Crashed", http.StatusInternalServerError) | |
return | |
} | |
} | |
log.Println("-----------------") | |
log.Println("\n" + string(req)) | |
io.Copy(w, bytes.NewBuffer(req)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment