Having problems with the v3 driver; getting the same 400 error despite following the template.
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.
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.
Most helpful comment
Hello @rdhara,
Please replace the
substitutionskey withdynamic_template_data. Also, in your HTML please use{{reset_token}}in your HTML and thenreset_tokenas the key in the request body.With Best Regards,
Elmer