使用请求混合表单和文件,带注释和可选字段。

huangapple go评论57阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年4月19日 21:34:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定