【问题标题】:Getting Bad Request Error while using sendgrid through python通过 python 使用 sendgrid 时出现错误的请求错误
【发布时间】:2019-10-04 20:54:04
【问题描述】:

我在尝试发送带有附件的电子邮件时收到来自 sendgrid 的错误请求

import base64
import os
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import (
    Mail, Attachment, FileContent, FileName,
    FileType, Disposition, ContentId)
import urllib.request as urllib
import os
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

message = Mail(
    from_email='xxx@gmail.com',
    to_emails='xxx@gmail.com',
    subject='Sending with Twilio SendGrid is Fun',
    html_content='<strong>and easy to do anywhere, even with Python</strong>')
file_path = 'C:\\Users\\xxx\\OneDrive\\Desktop\\xxx.pdf'
with open(file_path, 'rb') as f:
    data = f.read()
    f.close()
encoded = base64.b64encode(data).decode()
attachment = Attachment()
attachment.file_content = FileContent(encoded)
attachment.file_type = FileType('application/pdf')
attachment.file_name = FileName('test_filename.pdf')
attachment.disposition = Disposition('attachment')
attachment.content_id = ContentId('PDF Document file')
message.attachment = attachment
try:
    sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
    response = sendgrid_client.send(message)
    print(response.status_code)
    print(response.body)
    print(response.headers)
except Exception as e:
    print(e.message)
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "C:\Program Files (x86)\python\lib\site-packages\sendgrid\sendgrid.py", line 98, in send
    response = self.client.mail.send.post(request_body=message.get())
  File "C:\Program Files (x86)\python\lib\site-packages\python_http_client\client.py", line 262, in http_request
    self._make_request(opener, request, timeout=timeout)
  File "C:\Program Files (x86)\python\lib\site-packages\python_http_client\client.py", line 178, in _make_request
    raise exc
python_http_client.exceptions.BadRequestsError: HTTP Error 400: Bad Request

我假设它与我的 pdf 的编码有关。如何确保它是 base64 编码的?

【问题讨论】:

  • 能否请您包括错误消息和堆栈?

标签: python sendgrid


【解决方案1】:

不幸的是,即使这本身不是一个实际的“解决方案”,我也无法发表评论。 SendGrid 可能会与 BadRequest 一起发送更有帮助的消息,您可以使用 HTTPError 来查看,如下所示:

from python_http_client.exceptions import HTTPError

sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
try:
    response = sendgrid_client.send(message)
except HTTPError as e:
    print(e.to_dict)

来自:https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#error

希望这将帮助您真正解决问题!错误代码的完整列表可以在这里找到:https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html

我看不出您显示的代码有什么特别错误的地方。我今天遇到了这个问题,试图解决我自己的问题(绝对与您遇到的问题无关),但base64.b64encode(data).decode() 与我在使用 pdf 的工作示例中所遇到的完全相同。

【讨论】:

  • 这不是解决问题的正确答案,但它肯定有助于调试问题
  • 与“除了 python_http_client.exceptions.BadRequestsError as e:”配合得很好,谢谢!
猜你喜欢
  • 2020-04-17
  • 2020-01-12
  • 1970-01-01
  • 1970-01-01
  • 2021-04-03
  • 2018-04-22
  • 1970-01-01
  • 2018-03-25
  • 1970-01-01
相关资源
最近更新 更多