I have update my golang version from 1.9 to 1.11. After updating sendgrid mail send not working.
I have followed below link:
https://cloud.google.com/appengine/docs/standard/go111/go-differences
and found that we need to Use request.Context() or your preferred context instead of using appengine.NewContext . But when I am trying request.Context() getting request is not defined.
So how to change appengine.NewContext to request.Context() for go111
Here is my code:
func SendTestmail(c echo.Context) error {
type output struct {
Message string `json:"message"`
Status bool `json:"status"`
}
result := output{}
//mail code
to := "myemail@mail.com"
firstName := c.FormValue("first_name")
subject := "Send Test mail"
msg := "Dear User , " + "
" +
"You have successfully Tested." + "
" +
"Sincerely, " + "
" +
"Team"
var Body = general.FrameEmailObj(os.Getenv("SENDGRID_SENDER"), subject, to, msg)
url := os.Getenv("SENDGRID_URL")
req, err := http.NewRequest("POST", url, bytes.NewBuffer(Body))
req.Header.Set("Authorization", "Bearer "+os.Getenv("SENDGRID_API_KEY"))
req.Header.Set("Content-Type", "application/json")
ctx := appengine.NewContext(c.Request())
client := urlfetch.Client(ctx)
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
//end mail code
result.Message = "send mail success."
return c.JSON(http.StatusUnauthorized, result)
}
I am getting below error in appengine:-
PANIC RECOVER Post https://api.sendgrid.com/v3/mail/send: not an App Engine context goroutine
Thanks in advance for your help.
The migration document states that App Engine specific uflfetch package is superseded by the net/http package.
Replace this code:
ctx := appengine.NewContext(c.Request())
client := urlfetch.Client(ctx)
resp, err := client.Do(req)
with:
resp, err := http.DefaultClient.Do(req)