sendgrid-python icon indicating copy to clipboard operation
sendgrid-python copied to clipboard

Sendgrid: python_http_client.exceptions.BadRequestsError: HTTP Error 400: Bad Request

Open konidala opened this issue 6 years ago • 4 comments

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

konidala avatar May 18 '20 03:05 konidala

It's working fine.

konidala avatar May 18 '20 11:05 konidala

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.

devcrypted avatar Jun 08 '20 16:06 devcrypted

@kamal2k89 Recommend inspecting the thrown error. Example :https://github.com/sendgrid/sendgrid-python/blob/master/use_cases/error_handling.md

childish-sambino avatar Jun 16 '20 15:06 childish-sambino

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.

distvan avatar Sep 22 '20 11:09 distvan