更新时间:2022-04-22 22:20:43
您必须自己创建MIME编码.您可以使用 email.mime
软件包来做到这一点:
You'll have to create the MIME encoding yourself. You can do so with the email.mime
package:
import requests
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
related = MIMEMultipart('related')
submission = MIMEText('text', 'xml', 'utf8')
submission.set_payload(open('submission_set.xml', 'rb').read())
related.attach(submission)
document = MIMEText('text', 'plain')
document.set_payload(open('document.txt', 'rb').read())
related.attach(document)
body = related.as_string().split('\n\n', 1)[1]
headers = dict(related.items())
r = requests.post(url, data=body, headers=headers)
我假设XML文件使用UTF-8,您可能还想为document
条目设置一个字符集.
I presumed the XML file uses UTF-8, you probably want to set a character set for the document
entry as well.
requests
仅知道如何创建multipart/form-data
帖子正文; multipart/related
不常用.
requests
only knows how to create multipart/form-data
post bodies; the multipart/related
is not commonly used.