-
Notifications
You must be signed in to change notification settings - Fork 51
/
main.go.tpl
48 lines (40 loc) · 928 Bytes
/
main.go.tpl
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
package main
import (
"io"
"net/http"
)
func main() {
{{- if gt (len .Request.Form) 0 }}
data := url.Values{}
{{- range $key, $val := .Request.Form}}
data.Set("{{$key}}", "{{$val}}")
{{- end}}
body := strings.NewReader(data.Encode())
{{- else}}
body := bytes.NewBufferString("{{.Request.Body.String}}")
{{- end }}
req, err := http.NewRequest("{{.Request.Method}}", "{{.Request.API}}", body)
if err != nil {
panic(err)
}
{{- range $key, $val := .Request.Header}}
req.Header.Set("{{$key}}", "{{$val}}")
{{- end}}
{{- if gt (len .Request.Cookie) 0 }}
{{- range $key, $val := .Request.Cookie}}
req.AddCookie(&http.Cookie{
Name: "{{$key}}",
Value: "{{$val}}",
})
{{- end}}
{{- end}}
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
if resp.StatusCode != http.StatusOK {
panic("status code is not 200")
}
data, err := io.ReadAll(resp.Body)
println(string(data))
}