Jake
Jake

Reputation: 330

How do I convert this cURL code to python?

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

$ curl -F "[email protected]" 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'}

Upvotes: 0

Views: 52

Answers (2)

Nitin
Nitin

Reputation: 244

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

Upvotes: 1

furas
furas

Reputation: 142869

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())

Upvotes: 0

Related Questions