-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Debug the given input request
- Loading branch information
0 parents
commit 33d746a
Showing
3 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/alessiosavi/GoHttpDebugRequest | ||
|
||
go 1.14 | ||
|
||
require github.com/gorilla/mux v1.7.4 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= | ||
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"net/http/httputil" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
func DumpRequest(w http.ResponseWriter, req *http.Request) { | ||
requestDump, err := httputil.DumpRequest(req, true) | ||
if err != nil { | ||
fmt.Fprint(w, err.Error()) | ||
fmt.Println(err) | ||
} else { | ||
fmt.Fprint(w, string(requestDump)) | ||
fmt.Println(string(requestDump)) | ||
} | ||
} | ||
|
||
func main() { | ||
port := flag.Int("port", 9999, "Bind the server to the given port") | ||
host := flag.String("host", "localhost", "Bind the server to the given host") | ||
flag.Parse() | ||
if *port == 9999 && *host == "localhost" { | ||
flag.Usage() | ||
} | ||
|
||
router := mux.NewRouter() | ||
router.HandleFunc("/get", DumpRequest).Methods("GET") | ||
router.HandleFunc("/post", DumpRequest).Methods("POST") | ||
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", *host, *port), router)) | ||
} |