[英]Using data binary in Python requests
I am trying to represent the following curl statement in python: 我试图在python中表示以下curl语句:
curl --data-binary @sample.png --data project = 23423233 -H 'X-API-KEY: YOUR API KEY, User-Agent: AppName (name@example.com)' https://files.proofhub.com/files/upload
I have already done multiple post & get requests, however since this one uses the data option, I cannot get my head around how I would execute this using requests. 我已经完成了多个post和get请求,但是由于这个使用了数据选项,我无法理解如何使用请求执行此操作。
I will post my current code: 我将发布我当前的代码:
data = open(r"C:\Users\dlogan.CLEARDATA\Desktop\ProofHub Upload\test.txt",'rb')
create_headers = {'X-API-KEY': '', 'Content-Type': 'application/json', 'User-Agent': '@cleardata.co.uk'}
r = requests.post('https://cleardata.proofhub.com/files/upload', data=data, headers=create_headers)
Does anyone know how i'd go about including an file? 有谁知道如何包含文件?
The issue seems to be that you're missing a trailing slash '/' on the end of the URL. 问题似乎是你在URL的末尾错过了一个尾部斜杠'/'。 Without the trailing slash the server seems to redirect to a non-existent page and you get the 404.
如果没有尾部斜杠,服务器似乎会重定向到不存在的页面,您将获得404。
To fix, just add a trailing slash: 要修复,只需添加一个尾部斜杠:
requests.post('https://cleardata.proofhub.com/files/upload/', data=data, headers=create_headers)
# Add slash ^
I think you need to post your file as form data using the files
argument. 我认为您需要使用
files
参数将文件作为表单数据发布。
files = {'file': open(r"C:\Users\dlogan.CLEARDATA\Desktop\ProofHub Upload\test.txt",'rb')
}
create_headers = {'X-API-KEY': '', 'Content-Type': 'application/json', 'User-Agent': '@cleardata.co.uk'}
r = requests.post('https://cleardata.proofhub.com/files/upload', files=files, headers=create_headers)
You'll need to get the file name right - I can't see the post form, so I don't know what should be. 你需要正确的文件名 - 我看不到帖子的形式,所以我不知道应该是什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.