'How to upload a file to the file.io website using Python
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.
Solution 1:[1]
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
Solution 2:[2]
Tried in 2022:
import requests
from pathlib import Path
link=requests.post(url=r'https://file.io', files={'file': Path('string_path').read_bytes()})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |