我正在使用Python的requests库发送一个请求。当附件参数中包含非ASCII字符时,会引发异常。而在只有ASCII数据存在的其他情况下,一切都正常。
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'}
我收到了邮件,但没有附件。有什么想法吗?