System Information
-
Strapi Version: v4.2.0
-
Operating System: macOS
-
Database: mySQL
-
Node Version: 16.14.2
-
NPM Version: 8.5.0
-
Yarn Version: n/a
I want to upload files to strapi from a using image links. I am trying to upload images to using requests in python as follows:
url = strapi_url+"upload"
f = {'files': (name, 'image', {'uri': uri})} #here uri is the source file url
response = requests.post(url, files=f)
print(response)
The requests gives a 200 response code, but in the upload section on the dashboard files seems to have gotten corrupted. Here is how it looks:
Am I not doing it the right way, or is there any other issue that I might be facing here?
requirements.txt
Pillow==10.1.0
python-dotenv==1.0.0
requests==2.31.0
main.py
# Python version 3.12.0
# You have to install requirements.txt
# pip install -r requirements.txt
# Set ENV variables API_URL && API_SECRET
# API_SECRET is a custom strapi token (you can create from admin panel)
# API_URL === http://localhost:1337
from dotenv import load_dotenv
from PIL import Image
from pathlib import Path
import requests
def convert_image_to_webp(image_path: Path) -> Path:
# Convert image to webp and save file
image = Image.open(image_path)
image = image.convert("RGB")
image.save(image_path.with_suffix(".webp"), "webp", quality=92, optimize=True)
return image_path.with_suffix(".webp")
def upload_image(image_path: Path) -> None:
res = requests.post(
os.getenv("API_URL") + "/api/upload",
files={"files": (image_path.name, open(image_path, "rb"), "image/webp")},
headers={
"Authorization": f"Bearer {os.getenv('API_SECRET')}",
},
)
I am using this. I hope also helps to you