如何将此cURL代码转换为python?

时间:2019-07-24 11:53:32

标签: python python-3.x curl

所以我有一个来自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'}

2 个答案:

答案 0 :(得分:1)

请勿使用data参数发送文件。 有一个files参数,请尝试使用它。

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

检查是否可行。另请参阅HERE

答案 1 :(得分:0)

使用此门户网站https://curl.trillworks.com,我得到了工作代码。我测试了一些图片。

import requests

files = {
    'file': ('test.txt', open('test.txt', 'rb')),
}

response = requests.post('https://file.io/', files=files)

pritn(response.json())
相关问题