Sendgrid: python_http_client.exceptions.BadRequestsError: HTTP Error 400: Bad Request
Issue Summary
Getting BadRequestError while using sendgrid with dynamic_tempalte_data
Code Snippet
import sendgrid
import os
from sendgrid.helpers.mail import *
sg = sendgrid.SendGridAPIClient(api_key='****************************************')
from_email = Email("[email protected]")
to_email = To("[email protected]")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, to_email, subject, content)
mail.template_id = 'd - abd******'
mail.dynamic_template_data = {
'subject': 'Testing Templates',
'name': 'Some One',
'city': 'Denver'
}
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)
Exception/Log
Traceback (most recent call last):
File "test_sendgrid.py", line 152, in <module>
response = sg.client.mail.send.post(request_body=mail.get())
File "/home/puneetha/sendgrid_poc/venv3.6/lib/python3.6/site-packages/python_http_client/client.py", line 262, in http_request
self._make_request(opener, request, timeout=timeout)
File "/home/puneetha/sendgrid_poc/venv3.6/lib/python3.6/site-packages/python_http_client/client.py", line 178, in _make_request
raise exc
python_http_client.exceptions.BadRequestsError: HTTP Error 400: Bad Request
Technical details:
- sendgrid-python version: 6.3.1
- python version: 3.6
It's working fine.
what was the fix @konidala ? I'm getting the same error. But in my case, I'm able to send mail once, but not more then that. I stop the application and then able to send again.
@kamal2k89 Recommend inspecting the thrown error. Example :https://github.com/sendgrid/sendgrid-python/blob/master/use_cases/error_handling.md
what was the fix @konidala ? I'm getting the same error. But in my case, I'm able to send mail once, but not more then that. I stop the application and then able to send again.
Catch the exception and see the error message, maybe something wrong with your input.