Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better error handling #2

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 24 additions & 10 deletions go.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
{{#body.has_url_encoded_body}}
"net/url"
"bytes"
Expand All @@ -23,7 +24,7 @@ import (
{{/body.has_json_body}}
)

func send{{{codeSlug}}}() {
func send{{{codeSlug}}}() error {
// {{{request.name}}} ({{{request.method}}} {{{url.fullpath}}})

{{#body.has_raw_body}}
Expand Down Expand Up @@ -60,16 +61,22 @@ func send{{{codeSlug}}}() {
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)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return err
}

{{/body}}
{{^body}}
req, err := http.NewRequest("{{{request.method}}}", "{{{url.fullpath}}}", nil)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return err
}

{{/body}}
{{! ----- }}
Expand All @@ -88,24 +95,31 @@ func send{{{codeSlug}}}() {
{{! ----- }}
{{! Read params from url and add them }}
{{#url.has_params}}
parseFormErr := req.ParseForm()
if parseFormErr != nil {
fmt.Println(parseFormErr)
if err := req.ParseForm(); err != nil {
fmt.Fprintln(os.Stderr, err)
return err
}

{{/url.has_params}}
// Fetch Request
resp, err := client.Do(req)

resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("Failure : ", err)
fmt.Fprintln(os.Stderr, err)
return err
}
defer resp.Body.Close()

// Read Response Body
respBody, _ := ioutil.ReadAll(resp.Body)
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return err
}

// Display Results
fmt.Println("response Status : ", resp.Status)
fmt.Println("response Headers : ", resp.Header)
fmt.Println("response Body : ", string(respBody))

return nil
}