0

In my fastAPI application, i am defining a post api, where i have to receive files and its metadata from frontend. I will be receiving list of dictionary(or object) where dictionary will be having two field file and metadata. But i am getting error unprocessable entity.

Below is my python code

class FileItem(BaseModel):
    file: UploadFile = File(...)
    filetype: str = Form(...)

class FileItems(BaseModel):
    payload:list[FileItem]

@router.post("/testing/processfiles")
async def process_files(file_items: FileItems):
    for item in file_items:
        file = item.file
        filetype = item.filetype
        print(file,filetype)
    return {"message": "Files and strings processed successfully"} 

in swaggerUI the request body is of application/json instead of multipart/form-data.

Request body

{ "payload": [ { "file": "string", "filetype": "string" } ] }

when i pass list of object from the frontend with the same format, i got Upload error: 404 Unprocessable Entity. How can i achieve this ?

Jitaso
  • 1
  • 1
  • 1
    You can't compose a model like that when using HTTP post, instead you'll have to handle each parameter yourself: `process_files(files: List[UploadFile], filetype: List[str])` should do what you want - i.e. you'll have to have multiple form fields with the same name in that case; uploading files through standard post forms doesn't work together with JSON bodies. – MatsLindh Jun 05 '23 at 20:07

0 Answers0