diff --git a/go.mustache b/go.mustache index 0b7cc07..66e09aa 100644 --- a/go.mustache +++ b/go.mustache @@ -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)) }