4

Example

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)

Description

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.

Environment

  • OS: Linux: (Ubuntu 18.04)
  • FastAPI Version: 0.61.1
  • Requests Version: 2.24.0
  • Python Version: 3.7.5

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]}
0

2 Answers 2

5

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())
Sign up to request clarification or add additional context in comments.

Comments

0

You should be using the files argument for request module to send a file

import requests
import glob

path = glob.glob("test_folder/*", recursive=True) # a list of image's path

lst_img = []
for p in path[:3]:
    lst_img.append({"files": open(p, 'rb')})

url = "http://localhost:6789/" # url api of app

for data in lst_img:
    res = requests.post(url=url, files=data)
    print(res.status_code)
    print(res.text)

1 Comment

Thank u but I think i want to send multiple images simultaneously not each by each because FastAPI permit a list of images. If i wrap a for..loop then it's useless.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.