简体   繁体   中英

How do I convert this cURL code to python?

So I have this example from https://www.file.io/

$ curl -F "file=@test.txt" https://file.io

How do I use this in python? I tried this:

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)

And got {'success': False, 'error': 400, 'message': 'Trouble uploading file'}

Do not send the file in the data parameter. There is a files parameter, try using that.

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

Check this if it works. also refer HERE

Using this portal https://curl.trillworks.com I got working code. I tested with some image.

import requests

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

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

pritn(response.json())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM