首页 > 解决方案 > 如何将此 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'}

标签: pythonpython-3.xcurl

解决方案


不要在data参数中发送文件。有一个files参数,尝试使用它。

file = {'file': ('image.mp4', encoded_img)}
r = post(url='https://file.io', files=file)

检查它是否有效。也可以参考这里


推荐阅读