Is it possible to mix UploadFile with List of UploadFile in an endpoint? #12051
-
First Check
Commit to Help
Example Code@router.post("/upload",status_code=status.HTTP_201_CREATED)
def upload(
file_a: Annotated[UploadFile, File(description="File A")],
file_b: Annotated[UploadFile | None, File(description="Optional File B")] = None,
files_extra: Annotated[list[UploadFile] | None, File(description="Optional Extra Files")] = None
):
#DescriptionI'm trying to write an endpoint that where the first file is mandatory. I'm using the following to test the endpoint Using httpx, files = [
( "file_a", ("file_a.txt", open("file_a.txt", "rb"), "text/plain") ),
( "file_b", ("file_b.txt", open("file_b.txt", "rb"), "text/plain") )
[
( "files_extra", ("file_c.txt", open("file_c.txt", "rb"), "text/plain") ),
( "files_extra", ("file_d.txt", open("file_d.txt", "rb"), "text/plain") ),
( "files_extra", ("file_e.txt", open("file_e.txt", "rb"), "text/plain") )
]
]
response = httpx.post(url, files=files)
response.raise_for_status()But I get a 422 Error and this {'detail': [{'type': 'list_type',
'loc': ['body', 'other_files'],
'msg': 'Input should be a valid list',
'input': {'filename': 'file_c.txt',
'file': {'_file': {},
'_max_size': 1048576,
'_rolled': False,
'_TemporaryFileArgs': {'mode': 'w+b',
'buffering': -1,
'suffix': None,
'prefix': None,
'encoding': None,
'newline': None,
'dir': None,
'errors': None}},
'size': 1408,
'headers': {'content-disposition': 'form-data; name="files_extra"; filename="file_c.txt"',
'content-type': 'text/plain'}}}]}Operating SystemLinux Operating System DetailsNo response FastAPI Version0.112.0 Pydantic Version2.8.2 Python Version3.12.3 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
@guzman109 you need to replace files = [
( "file_a", ("file_a.txt", open("file_a.txt", "rb"), "text/plain") ),
( "file_b", ("file_b.txt", open("file_b.txt", "rb"), "text/plain") )
- [
+ # Each 'files_extra' is treated as a separate tuple, but they all share the same key
( "files_extra", ("file_c.txt", open("file_c.txt", "rb"), "text/plain") ),
( "files_extra", ("file_d.txt", open("file_d.txt", "rb"), "text/plain") ),
( "files_extra", ("file_e.txt", open("file_e.txt", "rb"), "text/plain") )
- ]
] |
Beta Was this translation helpful? Give feedback.
@guzman109 you need to replace
files_extra: Annotated[list[UploadFile] | None, File(description="Optional Extra Files")] = Nonetofiles_extra: Annotated[List[UploadFile], File(description="Optional Extra Files")] = []because the combination of List[UploadFile] | None with File(...) creates a conflict. FastAPI expects a required file list due to File(...), but if it receives None (as might be the case when files are not provided), it leads to processing issues.files = [ ( "file_a", ("file_a.txt", open("file_a.txt", "rb"), "text/plain") ), ( "file_b", ("file_b.txt", open("file_b.txt", "rb"), "text/plain") ) - [ + # Each 'files_extra' is treated as a separate tuple, but they all sh…