Fastapi: [BUG] Multiple files on swagger not work

Created on 23 May 2019  路  4Comments  路  Source: tiangolo/fastapi

Describe the bug
I'm trying to upload multiple files as documentation explaied:

@router.post("/uploadfile/")
def create_upload_files(files: List[UploadFile] = File(...)):
    return {"filenames": [file.filename for file in files]}

Expected behavior
then I test it on a swagger and expect to return names of files but I got this error:

422 | Error: Unprocessable Entity
Response body
{   
"detail": [  
   {    
   "loc": [     
    "body",     
    "files",    
     0    
   ],   
    "msg": "Expected UploadFile, received: <class 'str'>",    
   "type": "value_error"   
  } 
  ]
 }

Environment:

  • OS: [Windows]
  • FastAPI Version [0.20.0], get it with:
pip list
  • Python version [3.7.3], get it with:
python --version

Additional context
When I upload a single file as the code below:

@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
    return {"filename": file.filename}

the swagger works fine but when I use the code of multiple it's not working

bug

Most helpful comment

@muklah a work around that still lets you mostly make use of the swagger UI is to copy the curl request shown in the swagger UI for a single-file endpoint, and manually add additional -F arguments on the end with the desired filenames, and execute the curl command in a terminal -- that worked for me.

(Once you understand what the curl command should look like based on a single-file endpoint, you can see what the swagger UI is mis-generating, and can replace it with proper file locations)

All 4 comments

see the note in the docs : https://fastapi.tiangolo.com/tutorial/request-files/#multiple-file-uploads
you can use the HTMLResponse provided there

Yep, check the note linked by @euri10. It's a bug in Swagger UI. FastAPI supports it, and if you send multiple files it would work, but Swagger UI is not able to show the UI for that properly at the moment.

@muklah a work around that still lets you mostly make use of the swagger UI is to copy the curl request shown in the swagger UI for a single-file endpoint, and manually add additional -F arguments on the end with the desired filenames, and execute the curl command in a terminal -- that worked for me.

(Once you understand what the curl command should look like based on a single-file endpoint, you can see what the swagger UI is mis-generating, and can replace it with proper file locations)

Thanks all for your answers

Was this page helpful?
0 / 5 - 0 ratings