python - 在 Python 请求中使用数据二进制

标签 python curl request python-requests

我试图在 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

我已经完成了多个 post 和 get 请求,但是由于这个请求使用了数据选项,所以我不知道如何使用请求执行它。

我将发布我当前的代码:

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)

有谁知道我将如何包含一个文件?

最佳答案

问题似乎是您在 URL 末尾缺少尾部斜杠“/”。如果没有尾部斜杠,服务器似乎会重定向到一个不存在的页面,您会得到 404。

要修复,只需添加尾部斜杠:

requests.post('https://cleardata.proofhub.com/files/upload/', data=data, headers=create_headers)
#                                               Add slash ^

关于python - 在 Python 请求中使用数据二进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55748964/

相关文章:

c - 使用 libcurl 在 PUT 请求中发送字符串

python - 请求没有内容长度

python - 实现 SVM RBF

python - 小数精度在同一数据集上的工作方式不同

php - curl 多请求分割结果数组

php - 如何通过 OmniPay 将 curl 选项传递给 Guzzle?

php - Symfony2 - file_get_contents() 在同一项目 api url 上失败

facebook - FQL 应用程序限制错误

python - 如何在 Django 中给外键名称

python - 如何在预装 python 但没有 root 权限的 linux 上安装 Tkinter?