From 45937ab22e53ab4966affadf86b4d01dfe91366c Mon Sep 17 00:00:00 2001 From: Eddie Zaneski Date: Thu, 17 Jul 2014 12:54:05 -0400 Subject: [PATCH] Add useragent and set version to 0.5.1 --- sendgrid.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/sendgrid.go b/sendgrid.go index 35c25b97..db1c378d 100644 --- a/sendgrid.go +++ b/sendgrid.go @@ -7,9 +7,12 @@ import ( "net" "net/http" "net/url" + "strings" "time" ) +const Version = "0.5.1" + func timeoutHandler(network, address string) (net.Conn, error) { return net.DialTimeout(network, address, time.Duration(5*time.Second)) } @@ -87,18 +90,24 @@ func (sg *SGClient) Send(m *SGMail) error { if e != nil { return e } - r, e := sg.Client.PostForm(sg.APIMail, values) + req, e := http.NewRequest("POST", sg.APIMail, strings.NewReader(values.Encode())) + if e != nil { + return e + } + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + req.Header.Set("User-Agent", "sendgrid-go/"+Version+";go") + res, e := sg.Client.Do(req) if e != nil { - return fmt.Errorf("sendgrid.go: error:%v; response:%v", e, r) + return fmt.Errorf("sendgrid.go: error:%v; response:%v", e, res) } - - defer r.Body.Close() - - if r.StatusCode == http.StatusOK { + + defer res.Body.Close() + + if res.StatusCode == http.StatusOK { return nil } - - body, _ := ioutil.ReadAll(r.Body) - - return fmt.Errorf("sendgrid.go: code:%d error:%v body:%s", r.StatusCode, e, body) + + body, _ := ioutil.ReadAll(res.Body) + + return fmt.Errorf("sendgrid.go: code:%d error:%v body:%s", res.StatusCode, e, body) }