【发布时间】:2014-08-15 08:27:20
【问题描述】:
我正在使用 python 请求来发布请求。 当附件参数有一些非 ascii 字符时会引发异常,在其他只存在 ascii 数据的情况下,一切都很好。
you can see the exception here
response = requests.post(url="https://api.mailgun.net/v2/%s/messages" % utils.config.mailDomain,
auth=("api", utils.config.mailApiKey),
data={
"from" : me,
"to" : recepients,
"subject" : subject,
"html" if html else "text" : message
},
files= [('attachment', codecs.open(f.decode('utf8'))) for f in attachments] if attachments and len(attachments) else []
)
编辑: 使用 utf8 解码文件名后,我没有收到异常,但是文件未附加。 我通过附加一个名称中只有 ascii 字符的文件来调试请求,请求标头请求构建是:
{'Content-Type': None, 'Content-Location': None, 'Content-Disposition': u'form-data; name="attachment"; filename="Hello.docx"'}
成功了,我正在收到带有附件的邮件。
但是,当使用带有希伯来字符的文件时,请求的标头是:
{'Content-Type': None, 'Content-Location': None, 'Content-Disposition': 'form-data; name="attachment"; filename*=utf-8\'\'%D7%91%D7%93%D7%99%D7%A7%D7%94.doc'}
我收到了邮件,但没有附加文件。有什么想法吗?
【问题讨论】:
-
向我们展示错误跟踪。您提供的图片显示,有人尝试使用意外字符创建标题。但这可能是您的代码中有多个值的情况,stacktrace 会告诉我们更多信息。理想情况下,您应该提供一小段代码,它可以运行并显示问题。目前无法复制太多。
标签: python encoding decode python-requests mailgun