Sendgrid-python: 400 Bad Request Error when using substitution

Created on 25 Jul 2018  路  5Comments  路  Source: sendgrid/sendgrid-python

Issue Summary

Having problems with the v3 driver; getting the same 400 error despite following the template.

Steps to Reproduce

Here is the code I am using

sg = sendgrid.SendGridAPIClient(apikey=api_key)
from_email = sendgrid.Email(email="[email protected]", name="Boltzmann Support")
to_email = sendgrid.Email(email="[email protected]", name="Raghu")
content = Content("text/html", "text")
mail = Mail(from_email=from_email, subject="Welcome!", to_email=to_email, content=content)
mail.personalizations[0].add_substitution(Substitution(key="-reset_token-", value="sampletoken"))
mail.personalizations[0].add_substitution(
    Substitution(key="-email_to_reset-", value="[email protected]"))
mail.template_id = template_id

Printing mail.get() produces:

{'from': {'name': 'Boltzmann Support', 'email': '[email protected]'}, 'subject': 'Welcome!', 'personalizations': [{'to': [{'name': 'Raghu', 'email': '[email protected]'}], 'substitutions': {'-reset_token-': 'sampletoken', '-email_to_reset-': '[email protected]'}}], 'content': [{'type': 'text/html', 'value': 'text'}], 'template_id': REDACTED}

But I am met with BadRequestsError: HTTP Error 400: Bad Request. The email goes through when the I don't add any substitutions (commenting our those two personalization lines). The goal of substitution is to dynamically produce the contents of an href tag for the behavior of a button in the body of my email. Would appreciate any suggestions - thank you!

PS: Doesn't seem like the exception being raised is actually handled by the except, since the except catches urllib errors when the error is actually being raised from python_http_client.

Technical details:

  • sendgrid==5.4.1
  • Python Version: 3.6.5
duplicate

Most helpful comment

Hello @rdhara,

Please replace the substitutions key with dynamic_template_data. Also, in your HTML please use {{reset_token}} in your HTML and then reset_token as the key in the request body.

With Best Regards,

Elmer

All 5 comments

Hello @rdhara,

I'm assuming you are using our new dynamic templates as opposed to our legacy templates. The new dynamic templates are used a bit different and we are currently working on adding some helpers to this SDK to make it easy.

In the meantime, you can build your own request object like this.

With Best Regards,

Elmer

Hi @thinkingserious,

Thank you for your response. I am getting the same error even when using the method provided in your suggested resource.

data = {
  "content": [
    {
      "type": "text/html",
      "value": " "
    }
  ],
  "from": {
    "email": "[email protected]",
    "name": "Boltzmann Support"
  },
  "personalizations": [
    {
      "substitutions": {
        "-reset_token-": "sampletoken",
        "-email_to_reset-": "[email protected]"
      },
      "to": [
        {
          "email": "[email protected]",
        }
      ]
    }
  ],
  "subject": "Password Reset Request",
  "template_id": "REDACTED"
}

response = sg.client.mail.send.post(request_body=data)
print(response.status_code)
print(response.body)
print(response.headers)

returns the following response:

BadRequestsError                          Traceback (most recent call last)
<ipython-input-19-be118cda6c3d> in <module>()
     27 }
     28 
---> 29 response = sg.client.mail.send.post(request_body=data)
     30 print(response.status_code)
     31 print(response.body)

~/.virtualenvs/orca/lib/python3.6/site-packages/python_http_client/client.py in http_request(*_, **kwargs)
    250                 request.get_method = lambda: method
    251                 timeout = kwargs.pop('timeout', None)
--> 252                 return Response(self._make_request(opener, request, timeout=timeout))
    253             return http_request
    254         else:

~/.virtualenvs/orca/lib/python3.6/site-packages/python_http_client/client.py in _make_request(self, opener, request, timeout)
    174             exc = handle_error(err)
    175             exc.__cause__ = None
--> 176             raise exc
    177 
    178     def _(self, name):

BadRequestsError: HTTP Error 400: Bad Request

Is there some formatting issue I should know about? The returned error is not at all informative. Thank you!

Hello @rdhara,

Please replace the substitutions key with dynamic_template_data. Also, in your HTML please use {{reset_token}} in your HTML and then reset_token as the key in the request body.

With Best Regards,

Elmer

That worked - thank you!

Awesome!

Please follow this issue for progress on the creation of a helper.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DougCal picture DougCal  路  3Comments

lipis picture lipis  路  3Comments

MadReal picture MadReal  路  4Comments

andriisoldatenko picture andriisoldatenko  路  4Comments

hugomallinson picture hugomallinson  路  3Comments