-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestHeadersQueryParamsAndBody.go
executable file
·79 lines (64 loc) · 1.97 KB
/
requestHeadersQueryParamsAndBody.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"fmt"
"log"
"net/http"
"strings"
"os"
"io/ioutil"
)
func indexHandler(w http.ResponseWriter, r *http.Request) {
var bodyBytes []byte
var final string
var qparams, curlheaders string
final += "###################################################################\n"
final += "HTTP Method: "+r.Method+"\n"
final += "REQUEST URL: "+r.URL.Path+"\n"
if r.Body != nil {
bodyBytes, _ = ioutil.ReadAll(r.Body)
}
final += "BODY: "+string(bodyBytes)+"\n"
for key, val := range r.URL.Query() {
final += "Query Param: "+key+" = "+val[0]+"\n"
if qparams != "" {
qparams += "&"+key+"="+val[0]
} else {
qparams = "?"+key+"="+val[0]
}
}
qparams += "' \\\n"
for name, values := range r.Header {
for _, value := range values {
final += "HEADER: "+name+" = "+value+"\n"
if curlheaders != "" {
curlheaders += "\n-H '"+name+": "+value+"' \\"
} else {
curlheaders += "-H '"+name+": "+value+"' \\"
}
}
}
if string(bodyBytes) == "" {
final += "\nCURL COMMAND: \ncurl -X"+r.Method+" '{{host}}"+r.URL.Path+""+qparams+""+strings.TrimSuffix(curlheaders, "\\")+"\n\n"
} else {
final += "\nCURL COMMAND: \ncurl -X"+r.Method+" '{{host}}"+r.URL.Path+""+qparams+""+curlheaders+"--data-urlencode: '"+string(bodyBytes)+"'\n\n"
}
final += "###################################################################\n"
f, err := os.OpenFile("/tmp/requestHeadersQueryParamsAndBody.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
if _, err := f.Write([]byte(final)); err != nil {
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
fmt.Fprintf(w, string(bodyBytes))
}
func main() {
http.HandleFunc("/", indexHandler)
fmt.Printf("Starting server at port 5464\n")
if err := http.ListenAndServe(":5464", nil); err != nil {
log.Fatal(err)
}
}