sendgrid / sendgrid-python

The Official Twilio SendGrid Python API Library
https://sendgrid.com
MIT License
1.54k stars 711 forks source link

Not been able to delete an email from list of bounced emails #827

Closed hamidarrivy closed 5 years ago

hamidarrivy commented 5 years ago

I am working on a GAE(Google App Engine) based python app and which have sendgrid python SDK(v3.2.10) integrated into it. What I am trying do is right now that whenever sendgrid pushes an event webhook of type "bounce" I want to delete that bounced email from the list of bounced emails present on sendgrid.

I have already gone through the documentation provided on the official site. First I tried to delete email address using SDK and it worked fine on localhost. But after deploying it to the live server it just doesn't do anything and falls in the exception clause. Please see the code snippet below:

try:
    send_grid_client = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY)
    data = {"emails": [email.strip()]}
    delete_response = send_grid_client.client.suppression.bounces.delete(
                                    request_body=data)
except Exception as exception:
    logging.info('Exception is: {}'.format(exception))
    pass

As, it did not work as expected, Now I am trying to do the same using REST API. Please see the code snippet below:

import requests
data = {"emails": [email]}
headers = {"Authorization": "Bearer {}".format(SENDGRID_API_KEY)}
delete_response = requests.delete("https://api.sendgrid.com/v3/suppression/bounces", data=json.dumps(data), headers=headers)
logging.info(delete_response)
logging.info(delete_response.status_code)
logging.info(delete_response.text)

Now, sendgrid API is continuously returning error 400 with message {"errors":[{"field":null,"message":"emails or delete_all params required"}]}. I simply could not figure out how to overcome this issue. Maybe I am missing how to pass request body in the delete function but, I could not figure it out.

Any urgent help would be highly appreciated. Thanks.

white-bee commented 3 years ago

Having the same issue here. Was it resolve? Thank you.

white-bee commented 3 years ago

figured it out. Need to add Content-Type: application/json header that not mentioned on the API documentation page https://sendgrid.api-docs.io/v3.0/bounces-api/delete-bounces or https://sendgrid.api-docs.io/v3.0/how-to-use-the-sendgrid-v3-api/api-requests