我正在尝试使用requests模块重新编写一些旧的Python代码,目的是上传附件。邮件服务器要求进行以下规定:
https://api.elasticemail.com/attachments/upload?username=yourusername&api_key=yourapikey&file=yourfilename
能够工作的旧代码:
h = httplib2.Http()
resp, content = h.request('https://api.elasticemail.com/attachments/upload?username=omer&api_key=b01ad0ce&file=tmp.txt',
"PUT", body=file(filepath).read(),
headers={'content-type':'text/plain'} )
我找不到如何在请求中使用body部分。
我成功完成了以下操作:
response = requests.put('https://api.elasticemail.com/attachments/upload',
data={"file":filepath},
auth=('omer', 'b01ad0ce')
)
但是我不知道如何使用文件内容指定请求体。
谢谢您的帮助。 Omer.