-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
47 lines (43 loc) · 843 Bytes
/
main.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
func readBody(req *http.Request) string {
buf := new(bytes.Buffer)
buf.ReadFrom(req.Body)
return buf.String()
}
func handler(w http.ResponseWriter, r *http.Request) {
body := map[string]interface{}{
"method": r.Method,
"url": r.URL.Path,
}
reqHeader := make(map[string]string)
for key, val := range r.Header {
reqHeader[key] = val[0]
}
body["header"] = reqHeader
switch r.Method {
case "POST":
body["body"] = readBody(r)
break
case "PUT":
body["body"] = readBody(r)
break
}
data, err := json.Marshal(body)
if err != nil {
fmt.Printf("Error %s", err)
return
}
h := w.Header()
h.Set("Content-Type", "application/json; charset=UTF-8")
w.Write(data)
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}