trying a better way to preserve the req body bytes

This commit is contained in:
jackspirou 2015-11-20 19:29:09 -06:00
parent 5a591e800a
commit 099c8ee9aa

23
main.go
View File

@ -49,14 +49,12 @@ func main() {
// creates the payload. by default the payload // creates the payload. by default the payload
// is the build details in json format, but a custom // is the build details in json format, but a custom
// template may also be used. // template may also be used.
var buf *bytes.Buffer var buf bytes.Buffer
var reqBytes []byte
if len(vargs.Template) == 0 { if len(vargs.Template) == 0 {
var t bytes.Buffer if err := json.NewEncoder(&buf).Encode(&data); err != nil {
json.NewEncoder(&t).Encode(&data) fmt.Printf("Error encoding content template. %s\n", err)
b := t.Bytes() os.Exit(1)
buf = bytes.NewBuffer(b) }
reqBytes = b
} else { } else {
t, err := template.New("_").Parse(vargs.Template) t, err := template.New("_").Parse(vargs.Template)
@ -65,13 +63,10 @@ func main() {
os.Exit(1) os.Exit(1)
} }
if err := t.Execute(buf, &data); err != nil { if err := t.Execute(&buf, &data); err != nil {
fmt.Printf("Error executing content template. %s\n", err) fmt.Printf("Error executing content template. %s\n", err)
os.Exit(1) os.Exit(1)
} }
b := buf.Bytes()
buf = bytes.NewBuffer(b)
reqBytes = b
} }
// build and execute a request for each url. // build and execute a request for each url.
@ -87,7 +82,9 @@ func main() {
} }
// vargs.Method defaults to POST, no need to check // vargs.Method defaults to POST, no need to check
req, err := http.NewRequest(vargs.Method, uri.String(), buf) b := buf.Bytes()
r := bytes.NewReader(b)
req, err := http.NewRequest(vargs.Method, uri.String(), r)
if err != nil { if err != nil {
fmt.Printf("Error creating http request. %s\n", err) fmt.Printf("Error creating http request. %s\n", err)
os.Exit(1) os.Exit(1)
@ -132,7 +129,7 @@ func main() {
*/ */
// print out // print out
fmt.Printf("Webhook URL %d\n URL: %s\n METHOD: %s\n HEADERS: %s\n BODY: %s\n RESPONSE STATUS: %s\n RESPONSE: %s\n", i+1, req.URL, req.Method, req.Header, string(reqBytes), resp.Status, string(body)) fmt.Printf("Webhook URL %d\n URL: %s\n METHOD: %s\n HEADERS: %s\n BODY: %s\n RESPONSE STATUS: %s\n RESPONSE: %s\n", i+1, req.URL, req.Method, req.Header, string(b), resp.Status, string(body))
} }
} }
} }