sendgrid / sendgrid-python

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

Latest version breaks Quick Start Guide and possibly other documentation. #1001

Closed MrPanchoso closed 3 years ago

MrPanchoso commented 3 years ago

Issue Summary

With the latest release (6.8.0 as of this writing), this section of the quick start no longer works. The order of the parameters must be fixed or specify the arguments' name for each one.

Steps to Reproduce

  1. Attempt to follow the instructions described in this section to send a basic email.
  2. This error raises, due to the mismatch of the arguments' order: TypeError: Object of type Content is not JSON serializable
  3. Can be fixed by specifying the name for each argument like this:
    mail = Mail(from_email=from_email, to_emails=to_email, subject=subject, plain_text_content=content)

Code Snippet

import sendgrid
import os
from sendgrid.helpers.mail import *

sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("test@example.com")
to_email = To("test@example.com")
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)
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 "/Users/francisco/debugging/email_test.py", line 6896, in <module>
    response = sg.client.mail.send.post(request_body=mail.get())
  File "/Users/francisco/opt/anaconda3/lib/python3.8/site-packages/python_http_client/client.py", line 257, in http_request
    data = json.dumps(request_body).encode('utf-8')
  File "/Users/francisco/opt/anaconda3/lib/python3.8/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/Users/francisco/opt/anaconda3/lib/python3.8/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Users/francisco/opt/anaconda3/lib/python3.8/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/Users/francisco/opt/anaconda3/lib/python3.8/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Content is not JSON serializable
[Finished in 0.3s with exit code 1]

Technical details:

poctob commented 3 years ago

Also reproducible with: sendgrid-6.8.0 Python 3.9.1

Downgrading to sendgrid-6.7.1 resolves the issue