-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change indents to tabs (go's standard)
- Loading branch information
Showing
1 changed file
with
94 additions
and
94 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 |
---|---|---|
@@ -1,111 +1,111 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
{{#body.has_url_encoded_body}} | ||
"net/url" | ||
"bytes" | ||
"net/url" | ||
"bytes" | ||
{{/body.has_url_encoded_body}} | ||
{{#body.has_raw_body}} | ||
"strings" | ||
"strings" | ||
{{/body.has_raw_body}} | ||
{{#body.has_long_body}} | ||
"strings" | ||
"strings" | ||
{{/body.has_long_body}} | ||
{{#body.has_multipart_body}} | ||
"mime/multipart" | ||
"bytes" | ||
"mime/multipart" | ||
"bytes" | ||
{{/body.has_multipart_body}} | ||
{{#body.has_json_body}} | ||
"bytes" | ||
"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, _ := ioutil.ReadAll(resp.Body) | ||
|
||
// Display Results | ||
fmt.Println("response Status : ", resp.Status) | ||
fmt.Println("response Headers : ", resp.Header) | ||
fmt.Println("response Body : ", string(respBody)) | ||
// {{{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, _ := ioutil.ReadAll(resp.Body) | ||
|
||
// Display Results | ||
fmt.Println("response Status : ", resp.Status) | ||
fmt.Println("response Headers : ", resp.Header) | ||
fmt.Println("response Body : ", string(respBody)) | ||
} |