python - 如何将此 cURL 代码转换为 python?
问题描述
所以我有这个来自https://www.file.io/的例子
$ curl -F "file=@test.txt" https://file.io
我如何在 python 中使用它?我试过这个:
from requests import post
from urllib import request
from base64 import b64encode
with open('files/some_name.mp4', 'rb') as img:
encoded_img = b64encode(img.read())
r = post(url='https://file.io', data={'file' : encoded_img})
r = r.json()
print(r)
并得到{'success': False, 'error': 400, 'message': 'Trouble uploading file'}
解决方案
不要在data
参数中发送文件。有一个files
参数,尝试使用它。
file = {'file': ('image.mp4', encoded_img)}
r = post(url='https://file.io', files=file)
检查它是否有效。也可以参考这里
推荐阅读
- excel - Excel使用基于一个单元格的另一张表中的信息填充相应的单元格
- powershell - 如何将字符串操作为多个整数?
- php - 使用 jQuery POST 插入数据库
- android - 给出错误未连接适配器;跳过布局
- java - 来自 Firebase 的 ConcurrentModificationException
- android - TableRow 值中的 EditText 混淆了
- javascript - 节点 - 没有数据创建的 ES 索引
- apache - 更改的文档根在 xampp 中不起作用
- android - 如何在回收站视图中获取当前可见项目
- php - Amazon SES - 自动超出每日消息配额