Send file through POST without Content-Disposition in Python

此生再无相见时 提交于 2019-12-06 05:16:11

Ok, I was able to solve this. I will post my solution here if somebody has the same problem.

The solution was to use Prepared Requests (http://docs.python-requests.org/en/master/user/advanced/#prepared-requests) Then I could put the data into body in the form I needed. My code now looks like this:

headers = {'Content-Type': 'application/x-tar',                         
           'Content-Size': tar_size}

req = requests.Request('POST',
                       server,
                       headers)       

prepped = req.prepare()
with open(tar_name, 'rb') as f:
    prepped.body = fl.read(tar_size)

s = Request.Session()  
r = s.send(prepped,
           stream=True)                              

I had the same problem with unwanted Content-Disposition in the body of my POST. I solved it like this:

requests.post(server, headers=headers, data=open(myFile, 'rb').read())
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!