英文:
Using Request mixed Forms and Files with Annotation and optional fields
问题
I'd like to post mixed form fields and upload files to a FastAPI endpoint. The FastAPI documentation here states that you can mix params using Annotated
and the python-multipart
library e.g.
@router.post("/upload")
async def upload_contents(
an_int: Annotated[int, Form()],
a_string: Annotated[str, Form()],
some_files: Annotated[list[UploadFile], File()]
):
However if I want to make posting of a_string
optional, I have not found a way to make this work.
I have tried things like:
@router.post("/upload")
async def upload_contents(
an_int: Annotated[int, Form()],
a_string: Annotated[Union[str, None], Form()],
some_files: Annotated[list[UploadFile], File()]
):
And also on @flakes suggestion:
@router.post("/upload")
async def upload_contents(
an_int: Annotated[int, Form()],
a_string: Optional[Annotated[str], Form()]],
some_files: Annotated[list[UploadFile], File()]
):
While these run, the server is still requesting I provide the a_string
parameter. Any guidance would be gratefully received.
英文:
I'd like to post mixed form fields and upload files to a FastAPI endpoint. The FastAPI documentation here states that you can mix params using Annotated
and the python-multipart
library e.g.
@router.post("/upload")
async def upload_contents(
an_int: Annotated[int, Form()],
a_string: Annotated[str, Form()],
some_files: Annotated[list[UploadFile], File()]
):
However if I want to make posting of a_string
optional, I have not found a way to make this work.
I have tried things like:
@router.post("/upload")
async def upload_contents(
an_int: Annotated[int, Form()],
a_string: Annotated[Union[str, None], Form()],
some_files: Annotated[list[UploadFile], File()]
):
And also on @flakes suggestion:
@router.post("/upload")
async def upload_contents(
an_int: Annotated[int, Form()],
a_string: Optional[Annotated[str], Form()]],
some_files: Annotated[list[UploadFile], File()]
):
While these run, the server is still requesting I provide the a_string
parameter. Any guidance would be gratefully received.
答案1
得分: 2
以下是翻译好的内容:
这个问题在这个问题中讨论过。
基本上,如果在Form()
中设置了默认值,那么参数将变为可选的。
@router.post("/upload")
async def upload_contents(
an_int: Annotated[int, Form()],
some_files: Annotated[List[UploadFile], File()],
a_string: Union[str, None] = Form(default=None)
):
return True
看起来使用Annotated[Union[str, None], Form(default=None)]
不起作用,并会返回以下错误
AssertionError: 在 'a_string' 中无法在 'Annotated' 中设置 'Form' 默认值。请使用 '=' 来设置默认值。
英文:
It was discussed in this issue.
Basically, if the default value is set in Form()
, the parameter will be optional.
@router.post("/upload")
async def upload_contents(
an_int: Annotated[int, Form()],
some_files: Annotated[List[UploadFile], File()],
a_string: Union[str, None] = Form(default=None)
):
return True
It seems that using Annotated[Union[str, None], Form(default=None)]
isn't working and will return the following error
AssertionError: `Form` default value cannot be set in `Annotated` for 'a_string'. Set the default value with `=` instead.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论