我正在使用下面的请求库上传文件是代码:
files = {'file': open(full_file_name, 'rb')}
headers = {"content-type": 'application/x-www-form-urlencoded'}
final_resp = requests.put(loc, files=files, headers=headers)
问题是一些额外的内容已添加到文件的起点和终点。
添加到起点的内容是:
--b16010ae7646a031a5adc64ac0661e72
Content-Disposition: form-data; name="file"; filename="1016064585-65769268.csv"
添加到端点的内容是:
--b16010ae7646a031a5adc64ac0661e72--
但是当相同的文件通过postman上传时这些问题不会出现。
这是 postman 的截图 .
postman 的头是:
应用程序/x-www-form-urlencoded
最佳答案
这可能是因为您使用 multipart/form 上传文件。尝试使用如下代码所示的数据
data = open(localFilePath, 'rb').read()
headers = {
"Content-Type":"application/binary",
}
upload = requests.put(uploadUrl,data=data,headers=headers)
关于python-requests - 使用python请求上传二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54556859/