home‎ > ‎go‎ > ‎

dump-hhtp

This small program dumps the http request back to the browser:



/*
 Dump http request
*/
package main

import (
    "flag"
    "fmt"
"log"
"net/http"
u "net/http/httputil"
)

type dumpHandler struct {
}

func main() {
    inPort := flag.String("port","6060","Input Port Number, default 6060")
    flag.Parse()
     
    httpPort := ":" + *inPort
    fmt.Printf("Port %s, Will Dump the Request \n",httpPort)
     
// Dump it:
log.Fatal(http.ListenAndServe(httpPort, &dumpHandler{}))
}

func (h *dumpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
b, err := u.DumpRequest(r,true)
w.Header().Set("Content-Type", "text/plain; charset=utf-8") // normal header
if err != nil {
   w.WriteHeader(http.StatusInternalServerError)
   w.Write([]byte(err.Error()))
} else {
  w.WriteHeader(http.StatusOK)
       w.Write(b)
}
} 


To build and execute:
go build http-dump.go
./http-dump


ċ
http-dump.go
(1k)
Ashish Banerjee,
Jan 3, 2016, 6:38 AM
Comments