如何指定python requests的HTTP PUT请求体?

15 浏览
0 Comments

如何指定python requests的HTTP PUT请求体?

我正在尝试使用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'} )

我找不到如何在requests中使用body部分。

我设法做到了以下几点:

 response = requests.put('https://api.elasticemail.com/attachments/upload',
                    data={"file":filepath},                         
                     auth=('omer', 'b01ad0ce')                  
                     )

但是我不知道如何指定包含文件内容的body部分。

谢谢你的帮助。

Omer.

0