Here's my code trying to upload a list of images:
import requests
import glob
import cv2
path = glob.glob("test_folder/*", recursive=True) # a list of image's path
lst_img = []
for p in path[:3]:
# img = cv2.imread(p)
lst_img.append((p, open(p, 'rb'), "image/jpeg"))
data = {"files": lst_img}
url = "http://localhost:6789/" # url api of app
res = requests.post(url=url, data=data)
print(res.status_code)
print(res.text)
I am trying to upload a list of images through Python requests (package) to a FastAPI endpoint, but maybe my request's format is wrong, leading to a 422 error:
"detail":[{"loc":["body","files",0],"msg":"Expected UploadFile, received: <class 'str'>","type":"value_error"}
This is my request's format:
{'files': [('test_folder/image77.jpeg', <_io.BufferedReader name='test_folder/image77.jpeg'>, 'image/jpeg'), ('test_folder/image84.jpeg', <_io.BufferedReader name='test_folder/image84.jpeg'>, 'image/jpeg'), ('test_folder/image82.jpeg', <_io.BufferedReader name='test_folder/image82.jpeg'>, 'image/jpeg')]}
I've tried many ways, but always fails. Many thank if u guys help to solve it.
I tried the below, but still not working:
lst_img.append(("file", (p, open(p, 'rb'), "image/jpeg")))
My FastAPI main.py
from typing import List
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import StreamingResponse, FileResponse
app = FastAPI()
@app.post("/")
async def main(files: List[UploadFile] = File(...)):
# file_like = open(video_path, mode="rb")
# return StreamingResponse(file_like, media_type="video/mp4")
return {"filenames": [file.filename for file in files]}
Below is an example of how to upload multiple files (images) using Python requests and FastAPI. If you need to send additional (Form or JSON) data when uploading the files, please have a look this answer. Also, if you need async file reading and/or writing of the images on server side, please have a look at this answer.
app.py
from fastapi import FastAPI, File, UploadFile, HTTPException, status
from typing import List
app = FastAPI()
@app.post("/upload")
def upload(files: List[UploadFile] = File(...)):
for file in files:
try:
contents = file.file.read()
with open(file.filename, 'wb') as f:
f.write(contents)
except Exception:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail='There was an error uploading the file')
finally:
file.file.close()
return {"message": f"Successfuly uploaded {[file.filename for file in files]}"}
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='0.0.0.0', port=8000)
test.py
import requests
import glob
paths = glob.glob("images/*", recursive=True) # returns a list of file paths
images = [('files', open(p, 'rb')) for p in paths] # or paths[:3] to select the first 3 images
url = 'http://127.0.0.1:8000/upload'
resp = requests.post(url=url, files=images)
print(resp.json())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With