-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgo.mustache
111 lines (94 loc) · 2.41 KB
/
go.mustache
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"fmt"
"io"
"net/http"
{{#body.has_url_encoded_body}}
"net/url"
"bytes"
{{/body.has_url_encoded_body}}
{{#body.has_raw_body}}
"strings"
{{/body.has_raw_body}}
{{#body.has_long_body}}
"strings"
{{/body.has_long_body}}
{{#body.has_multipart_body}}
"mime/multipart"
"bytes"
{{/body.has_multipart_body}}
{{#body.has_json_body}}
"bytes"
{{/body.has_json_body}}
)
func send{{{codeSlug}}}() {
// {{{request.name}}} ({{{request.method}}} {{{url.fullpath}}})
{{#body.has_raw_body}}
body := strings.NewReader(`{{{body.raw_body}}}`)
{{/body.has_raw_body}}
{{! ----- }}
{{#body.has_long_body}}
body := strings.NewReader(`set your body string`)
{{/body.has_long_body}}
{{! ----- }}
{{#body.has_url_encoded_body}}
params := url.Values{}
{{#body.url_encoded_body}}
params.Set("{{{name}}}", "{{{value}}}")
{{/body.url_encoded_body}}
body := bytes.NewBufferString(params.Encode())
{{/body.has_url_encoded_body}}
{{! ----- }}
{{#body.has_multipart_body}}
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
{{#body.multipart_body}}
writer.WriteField("{{{name}}}","{{{value}}}")
{{/body.multipart_body}}
writer.Close()
{{/body.has_multipart_body}}
{{! ----- }}
{{#body.has_json_body}}
json := []byte(`{{{body.json_body_object}}}`)
body := bytes.NewBuffer(json)
{{/body.has_json_body}}
// Create client
client := &http.Client{}
// Create request
{{#body}}
req, err := http.NewRequest("{{{request.method}}}", "{{{url.fullpath}}}", body)
{{/body}}
{{^body}}
req, err := http.NewRequest("{{{request.method}}}", "{{{url.fullpath}}}", nil)
{{/body}}
{{! ----- }}
{{#headers.has_headers}}
// Headers
{{#headers.header_list}}
req.Header.Add("{{{header_name}}}", "{{{header_value}}}")
{{/headers.header_list}}
{{/headers.has_headers}}
{{! ----- }}
{{#body.has_multipart_body}}
req.Header.Add("Content-Type", writer.FormDataContentType())
{{/body.has_multipart_body}}
{{! ----- }}
{{! Read params from url and add them }}
{{#url.has_params}}
parseFormErr := req.ParseForm()
if parseFormErr != nil {
fmt.Println(parseFormErr)
}
{{/url.has_params}}
// Fetch Request
resp, err := client.Do(req)
if err != nil {
fmt.Println("Failure : ", err)
}
// Read Response Body
respBody, _ := io.ReadAll(resp.Body)
// Display Results
fmt.Println("response Status : ", resp.Status)
fmt.Println("response Headers : ", resp.Header)
fmt.Println("response Body : ", string(respBody))
}