How to upload a file to the file.io website using Python

Question:

I am trying to send a POST request to the file.io website to upload a file from my Python script and I want to get the link for that file. Here is my code:

import requests

url = 'https://file.io/post'
files = {"file": open("myimage.jpg", "rb")}
response = requests.post(url, files=files)
link = response.json()['link']

print(link)

When I print the response code, I get 200. Everything seems OK, but I can’t get the link of the uploaded file. I get an error.
I want to be able to upload a file I specify and get back a link using my little Python script.

Asked By: Sahar

||

Answers:

This is what I tried. I think your URL should not contains /post

import requests

url = 'https://file.io/'
data = {
    "file": open("myimage.png", "rb"),
    "maxDownloads": 100,
    "autoDelete": True
}
response = requests.post(url, files=data)
res = response.json()
print(res)
print(res["link"])

and results in

{'success': True, 'status': 200, 'id': '800e35c0-01eb-11ec-95c3-335b9c818244', 'key': 'zZRteNaopzbt', 'name': 'myimage.png', 'link': 'https://file.io/zZRteNaopzbt', 'private': False, 'expires': '2021-09-03T19:19:11.898Z', 'downloads': 0, 'maxDownloads': 1, 'autoDelete': True, 'size': 1552900, 'mimeType': 'text/plain', 'created': '2021-08-20T19:19:11.898Z', 'modified': '2021-08-20T19:19:11.898Z'}
https://file.io/zZRteNaopzbt
Answered By: Rishabh Deep Singh

Tried in 2022:

import requests
from pathlib import Path

link=requests.post(url=r'https://file.io', files={'file': Path('string_path').read_bytes()})
Answered By: Alex Deft

For the one who concerned: I had written a wrapper for file.io:

Install

pip install fileio-wrapper

Upload without authenticate

from fileio_wrapper import Fileio

resp = Fileio.upload(filepath)
success = resp['success']  # True if upload was successful
status = resp['status']  # HTTP status code
key = resp['key']  # ID of uploaded file
link = resp['link']  # Link to file (not a direct link)

Upload with authenticate

from fileio_wrapper import Fileio

fileio = Fileio(fileio_api_key)
resp = fileio.upload(filepath)
success = resp['success']  # True if upload was successful
status = resp['status']  # HTTP status code
key = resp['key']  # ID of uploaded file
link = resp['link']  # Link to file (not a direct link)

More detail (Config, Update, Delete, Download, etc…)

Give me a star for the project and I’ll be happy.
https://github.com/chumicat/fileio_wrapper

Answered By: Chumicat
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.