Closed john0017 closed 3 years ago
Can you post the stack trace of the exception?
Can you post the stack trace of the exception?
Traceback (most recent call last): File "--path--/backupScript.py", line 37, in response = sg.client.mail.send.post(request_body=mail.get()) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/python_http_client/client.py", line 257, in http_request data = json.dumps(request_body).encode('utf-8') File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/init.py", line 231, in dumps return _default_encoder.encode(obj) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/encoder.py", line 199, in encode chunks = self.iterencode(o, _one_shot=True) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/encoder.py", line 257, in iterencode return _iterencode(o, 0) File "/Library/Frameworks/Python.framework/Versions/3.8/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
Can you post the stack trace of the exception?
Hi,
Any thoughts on this?
How is the variable csv
being defined?
How is the variable
csv
being defined?
Thank you for your response. The code works fine on jupiter notebook but not on Pycharm or VS code.
The csv file is defined as -
with open('filename.csv', 'rb') as f: data = f.read() csv = base64.b64encode(data).decode()
Can you please print the output of printing the request body and post it here? Make sure to redact sensitive information. You can follow our troubleshooting guide for guidance on how to properly print the request body.
Can you please print the output of printing the request body and post it here? Make sure to redact sensitive information. You can follow our troubleshooting guide for guidance on how to properly print the request body.
Is this what you are looking for?
{ "attachments": [ { "content":"--csv in bytes--", "disposition": "attachment", "filename": "test.csv", "type": "text/csv" } ], "content": [ { "type": "text/plain", "value": "and easy to do anywhere, even with Python" } ], "from": { "email": "abc@mail.com" }, "personalizations": [ { "to": [ { "email": "xyz@mail.com" } ] } ], "subject": "Sending with SendGrid is Fun" }
Can you please print the output of printing the request body and post it here? Make sure to redact sensitive information. You can follow our troubleshooting guide for guidance on how to properly print the request body.
hi, any update?
Hi @john0017, Can you update to the latest version of the sendgrid-python library v6.8.1 and try re-running your code to see if you still see the JSON serializable error?
Hi @john0017, Can you update to the latest version of the sendgrid-python library v6.8.1 and try re-running your code to see if you still see the JSON serializable error?
Thank you for your response. However, that did not work either. I updated the script by not using the Mail helper class and that seems to work.
`sg = sendgrid.SendGridAPIClient(api_key=API_KEY)
from_email = Email("xzy@abc.com") to_email = To("fgh@abc.com") subject = "Sending with SendGrid is Fun" content = Content("text/plain", "hi")
attachment = Attachment() attachment.file_content = FileContent(csv) attachment.file_type = FileType('text/csv') attachment.file_name = FileName('test.csv') attachment.disposition = Disposition('attachment')
mail = Mail(from_email, to_email, subject, content) mail.attachment = attachment
response = sg.client.mail.send.post(request_body=mail.get()) `print(response.status_code)``
The csv above is a pandas df converted to csv and then encoded.
The code works fine on jupyter notebook, however on VScode/ Pycharm i get the error "TypeError: Object of type Content is not JSON serializable"