跳到内容

请求参数

以下是关于请求参数的参考信息。

这些是特殊的函数,您可以将其放在路径操作函数的参数中,或与 Annotated 一起使用在依赖函数中,以从请求中获取数据。

它包含:

  • Query()
  • Path()
  • Body()
  • Cookie()
  • Header()
  • Form()
  • File()

您可以直接从 fastapi 中导入它们。

from fastapi import Body, Cookie, File, Form, Header, Path, Query

fastapi.Query

Query(
    default=Undefined,
    *,
    default_factory=_Unset,
    alias=None,
    alias_priority=_Unset,
    validation_alias=None,
    serialization_alias=None,
    title=None,
    description=None,
    gt=None,
    ge=None,
    lt=None,
    le=None,
    min_length=None,
    max_length=None,
    pattern=None,
    regex=None,
    discriminator=None,
    strict=_Unset,
    multiple_of=_Unset,
    allow_inf_nan=_Unset,
    max_digits=_Unset,
    decimal_places=_Unset,
    examples=None,
    example=_Unset,
    openapi_examples=None,
    deprecated=None,
    include_in_schema=True,
    json_schema_extra=None,
    **extra
)
参数 描述
default

如果未设置参数字段,则使用的默认值。

FastAPI 关于查询参数的文档 中了解更多信息。

类型: Any 默认值: Undefined

default_factory

用于生成默认值的可调用对象(callable)。

这不会影响 Path 参数,因为该值总是必需的。此参数仅用于兼容性。

类型: Callable[[], Any] | None 默认值: _Unset

alias

参数字段的别名。

这将用于提取数据以及生成的 OpenAPI 中。当您因为 Python 保留关键字等原因无法使用想要的名称时,它特别有用。

FastAPI 关于查询参数的文档 中了解更多信息。

类型: str | None 默认值: None

alias_priority

别名的优先级。这会影响是否使用别名生成器。

类型: int | None 默认值: _Unset

validation_alias

“白名单”验证步骤。参数字段将是别名或定义的别名集中唯一允许的那个。

类型: str | AliasPath | AliasChoices | None 默认值: None

serialization_alias

“黑名单”验证步骤。原始参数字段将是别名或别名集字段中的唯一一个,所有其他字段在序列化时将被忽略。

类型: str | None 默认值: None

title

人类可读的标题。

FastAPI 关于查询参数的文档 中了解更多信息。

类型: str | None 默认值: None

description

人类可读的描述。

FastAPI 关于查询参数的文档 中了解更多信息。

类型: str | None 默认值: None

gt

大于。如果设置,值必须大于此值。仅适用于数字。

FastAPI 关于路径参数数值验证的文档 中了解更多信息。

类型: float | None 默认值: None

ge

大于或等于。如果设置,值必须大于或等于此值。仅适用于数字。

FastAPI 关于路径参数数值验证的文档 中了解更多信息。

类型: float | None 默认值: None

lt

小于。如果设置,值必须小于此值。仅适用于数字。

FastAPI 关于路径参数数值验证的文档 中了解更多信息。

类型: float | None 默认值: None

le

小于或等于。如果设置,值必须小于或等于此值。仅适用于数字。

FastAPI 关于路径参数数值验证的文档 中了解更多信息。

类型: float | None 默认值: None

min_length

字符串的最小长度。

FastAPI 关于查询参数的文档 中了解更多信息。

类型: int | None 默认值: None

max_length

字符串的最大长度。

FastAPI 关于查询参数的文档 中了解更多信息。

类型: int | None 默认值: None

pattern

字符串的正则表达式模式。

在 [FastAPI 关于查询参数的文档](https://fastapi.org.cn/tutorial/query-params-str-validations/#add-regular-expressions) 中了解更多信息。

类型: str | None 默认值: None

regex

在 FastAPI 0.100.0 和 Pydantic v2 中已弃用,请改用 pattern。字符串的正则表达式模式。

类型: str | None 默认值: None

discriminator

用于在带标签的联合类型(tagged union)中区分类型的参数字段名称。

类型: str | None 默认值: None

strict

如果为 True,则对该字段应用严格验证。

类型: bool | None 默认值: _Unset

multiple_of

值必须是此数字的倍数。仅适用于数字。

类型: float | None 默认值: _Unset

allow_inf_nan

允许 inf-infnan。仅适用于数字。

类型: bool | None 默认值: _Unset

max_digits

十进制数值允许的最大位数。

类型: int | None 默认值: _Unset

decimal_places

十进制数值允许的小数位数。

类型: int | None 默认值: _Unset

examples

此字段的示例值。

FastAPI 关于声明请求示例数据的文档 中了解更多信息。

类型: list[Any] | None 默认值: None

example

在 OpenAPI 3.1.0 中已弃用(现已使用 JSON Schema 2020-12),尽管仍受支持。请改用 examples。

类型: Any | None 默认值: _Unset

openapi_examples

OpenAPI 特有的示例。

它将被添加到生成的 OpenAPI (例如在 /docs 中可见)。

Swagger UI(提供 /docs 界面)对 OpenAPI 特有示例的支持比对 JSON Schema examples 的支持更好,这是其主要应用场景。

FastAPI 关于声明请求示例数据的文档 中了解更多信息。

类型: dict[str, Example] | None 默认值: None

deprecated

将此参数字段标记为已弃用。

它会影响生成的 OpenAPI(例如在 /docs 中可见)。

FastAPI 关于查询参数的文档 中了解更多信息。

类型: deprecated | str | bool | None 默认值: None

include_in_schema

用于决定是否在生成的 OpenAPI 中包含此参数字段。您可能不需要它,但它是可用的。

这会影响生成的 OpenAPI (例如在 /docs 中可见)。

在 [FastAPI 关于查询参数的文档](https://fastapi.org.cn/tutorial/query-params-str-validations/#exclude-parameters-from-openapi) 中了解更多信息。

类型: bool 默认值: True

json_schema_extra

任何额外的 JSON schema 数据。

类型: dict[str, Any] | None 默认值: None

**extra

extra 参数已弃用。请改用 json_schema_extra。包含 JSON Schema 使用的额外字段。

类型: Any 默认值: {}

源代码位于 fastapi/param_functions.py
def Query(  # noqa: N802
    default: Annotated[
        Any,
        Doc(
            """
            Default value if the parameter field is not set.

            Read more about it in the
            [FastAPI docs about Query parameters](https://fastapi.org.cn/tutorial/query-params-str-validations/#alternative-old-query-as-the-default-value)
            """
        ),
    ] = Undefined,
    *,
    default_factory: Annotated[
        Callable[[], Any] | None,
        Doc(
            """
            A callable to generate the default value.

            This doesn't affect `Path` parameters as the value is always required.
            The parameter is available only for compatibility.
            """
        ),
    ] = _Unset,
    alias: Annotated[
        str | None,
        Doc(
            """
            An alternative name for the parameter field.

            This will be used to extract the data and for the generated OpenAPI.
            It is particularly useful when you can't use the name you want because it
            is a Python reserved keyword or similar.

            Read more about it in the
            [FastAPI docs about Query parameters](https://fastapi.org.cn/tutorial/query-params-str-validations/#alias-parameters)
            """
        ),
    ] = None,
    alias_priority: Annotated[
        int | None,
        Doc(
            """
            Priority of the alias. This affects whether an alias generator is used.
            """
        ),
    ] = _Unset,
    validation_alias: Annotated[
        str | AliasPath | AliasChoices | None,
        Doc(
            """
            'Whitelist' validation step. The parameter field will be the single one
            allowed by the alias or set of aliases defined.
            """
        ),
    ] = None,
    serialization_alias: Annotated[
        str | None,
        Doc(
            """
            'Blacklist' validation step. The vanilla parameter field will be the
            single one of the alias' or set of aliases' fields and all the other
            fields will be ignored at serialization time.
            """
        ),
    ] = None,
    title: Annotated[
        str | None,
        Doc(
            """
            Human-readable title.

            Read more about it in the
            [FastAPI docs about Query parameters](https://fastapi.org.cn/tutorial/query-params-str-validations/#declare-more-metadata)
            """
        ),
    ] = None,
    description: Annotated[
        str | None,
        Doc(
            """
            Human-readable description.

            Read more about it in the
            [FastAPI docs about Query parameters](https://fastapi.org.cn/tutorial/query-params-str-validations/#declare-more-metadata)
            """
        ),
    ] = None,
    gt: Annotated[
        float | None,
        Doc(
            """
            Greater than. If set, value must be greater than this. Only applicable to
            numbers.

            Read more about it in the
            [FastAPI docs about Path parameters numeric validations](https://fastapi.org.cn/tutorial/path-params-numeric-validations/#number-validations-greater-than-and-less-than-or-equal)
            """
        ),
    ] = None,
    ge: Annotated[
        float | None,
        Doc(
            """
            Greater than or equal. If set, value must be greater than or equal to
            this. Only applicable to numbers.

            Read more about it in the
            [FastAPI docs about Path parameters numeric validations](https://fastapi.org.cn/tutorial/path-params-numeric-validations/#number-validations-greater-than-and-less-than-or-equal)
            """
        ),
    ] = None,
    lt: Annotated[
        float | None,
        Doc(
            """
            Less than. If set, value must be less than this. Only applicable to numbers.

            Read more about it in the
            [FastAPI docs about Path parameters numeric validations](https://fastapi.org.cn/tutorial/path-params-numeric-validations/#number-validations-greater-than-and-less-than-or-equal)
            """
        ),
    ] = None,
    le: Annotated[
        float | None,
        Doc(
            """
            Less than or equal. If set, value must be less than or equal to this.
            Only applicable to numbers.

            Read more about it in the
            [FastAPI docs about Path parameters numeric validations](https://fastapi.org.cn/tutorial/path-params-numeric-validations/#number-validations-greater-than-and-less-than-or-equal)
            """
        ),
    ] = None,
    min_length: Annotated[
        int | None,
        Doc(
            """
            Minimum length for strings.

            Read more about it in the
            [FastAPI docs about Query parameters](https://fastapi.org.cn/tutorial/query-params-str-validations/)
            """
        ),
    ] = None,
    max_length: Annotated[
        int | None,
        Doc(
            """
            Maximum length for strings.

            Read more about it in the
            [FastAPI docs about Query parameters](https://fastapi.org.cn/tutorial/query-params-str-validations/)
            """
        ),
    ] = None,
    pattern: Annotated[
        str | None,
        Doc(
            """
            RegEx pattern for strings.

            Read more about it in the
            [FastAPI docs about Query parameters](https://fastapi.org.cn/tutorial/query-params-str-validations/#add-regular-expressions
            """
        ),
    ] = None,
    regex: Annotated[
        str | None,
        Doc(
            """
            RegEx pattern for strings.
            """
        ),
        deprecated(
            "Deprecated in FastAPI 0.100.0 and Pydantic v2, use `pattern` instead."
        ),
    ] = None,
    discriminator: Annotated[
        str | None,
        Doc(
            """
            Parameter field name for discriminating the type in a tagged union.
            """
        ),
    ] = None,
    strict: Annotated[
        bool | None,
        Doc(
            """
            If `True`, strict validation is applied to the field.
            """
        ),
    ] = _Unset,
    multiple_of: Annotated[
        float | None,
        Doc(
            """
            Value must be a multiple of this. Only applicable to numbers.
            """
        ),
    ] = _Unset,
    allow_inf_nan: Annotated[
        bool | None,
        Doc(
            """
            Allow `inf`, `-inf`, `nan`. Only applicable to numbers.
            """
        ),
    ] = _Unset,
    max_digits: Annotated[
        int | None,
        Doc(
            """
            Maximum number of digits allowed for decimal values.
            """
        ),
    ] = _Unset,
    decimal_places: Annotated[
        int | None,
        Doc(
            """
            Maximum number of decimal places allowed for decimal values.
            """
        ),
    ] = _Unset,
    examples: Annotated[
        list[Any] | None,
        Doc(
            """
            Example values for this field.

            Read more about it in the
            [FastAPI docs for Declare Request Example Data](https://fastapi.org.cn/tutorial/schema-extra-example/)
            """
        ),
    ] = None,
    example: Annotated[
        Any | None,
        deprecated(
            "Deprecated in OpenAPI 3.1.0 that now uses JSON Schema 2020-12, "
            "although still supported. Use examples instead."
        ),
    ] = _Unset,
    openapi_examples: Annotated[
        dict[str, Example] | None,
        Doc(
            """
            OpenAPI-specific examples.

            It will be added to the generated OpenAPI (e.g. visible at `/docs`).

            Swagger UI (that provides the `/docs` interface) has better support for the
            OpenAPI-specific examples than the JSON Schema `examples`, that's the main
            use case for this.

            Read more about it in the
            [FastAPI docs for Declare Request Example Data](https://fastapi.org.cn/tutorial/schema-extra-example/#using-the-openapi_examples-parameter).
            """
        ),
    ] = None,
    deprecated: Annotated[
        deprecated | str | bool | None,
        Doc(
            """
            Mark this parameter field as deprecated.

            It will affect the generated OpenAPI (e.g. visible at `/docs`).

            Read more about it in the
            [FastAPI docs about Query parameters](https://fastapi.org.cn/tutorial/query-params-str-validations/#deprecating-parameters)
            """
        ),
    ] = None,
    include_in_schema: Annotated[
        bool,
        Doc(
            """
            To include (or not) this parameter field in the generated OpenAPI.
            You probably don't need it, but it's available.

            This affects the generated OpenAPI (e.g. visible at `/docs`).

            Read more about it in the
            [FastAPI docs about Query parameters](https://fastapi.org.cn/tutorial/query-params-str-validations/#exclude-parameters-from-openapi
            """
        ),
    ] = True,
    json_schema_extra: Annotated[
        dict[str, Any] | None,
        Doc(
            """
            Any additional JSON schema data.
            """
        ),
    ] = None,
    **extra: Annotated[
        Any,
        Doc(
            """
            Include extra fields used by the JSON Schema.
            """
        ),
        deprecated(
            """
            The `extra` kwargs is deprecated. Use `json_schema_extra` instead.
            """
        ),
    ],
) -> Any:
    return params.Query(
        default=default,
        default_factory=default_factory,
        alias=alias,
        alias_priority=alias_priority,
        validation_alias=validation_alias,
        serialization_alias=serialization_alias,
        title=title,
        description=description,
        gt=gt,
        ge=ge,
        lt=lt,
        le=le,
        min_length=min_length,
        max_length=max_length,
        pattern=pattern,
        regex=regex,
        discriminator=discriminator,
        strict=strict,
        multiple_of=multiple_of,
        allow_inf_nan=allow_inf_nan,
        max_digits=max_digits,
        decimal_places=decimal_places,
        example=example,
        examples=examples,
        openapi_examples=openapi_examples,
        deprecated=deprecated,
        include_in_schema=include_in_schema,
        json_schema_extra=json_schema_extra,
        **extra,
    )

fastapi.Path

Path(
    default=...,
    *,
    default_factory=_Unset,
    alias=None,
    alias_priority=_Unset,
    validation_alias=None,
    serialization_alias=None,
    title=None,
    description=None,
    gt=None,
    ge=None,
    lt=None,
    le=None,
    min_length=None,
    max_length=None,
    pattern=None,
    regex=None,
    discriminator=None,
    strict=_Unset,
    multiple_of=_Unset,
    allow_inf_nan=_Unset,
    max_digits=_Unset,
    decimal_places=_Unset,
    examples=None,
    example=_Unset,
    openapi_examples=None,
    deprecated=None,
    include_in_schema=True,
    json_schema_extra=None,
    **extra
)

路径操作声明一个路径参数。

FastAPI 关于路径参数和数值验证的文档 中了解更多信息。

from typing import Annotated

from fastapi import FastAPI, Path

app = FastAPI()


@app.get("/items/{item_id}")
async def read_items(
    item_id: Annotated[int, Path(title="The ID of the item to get")],
):
    return {"item_id": item_id}
参数 描述
default

如果未设置参数字段,则使用的默认值。

这不会影响 Path 参数,因为该值总是必需的。此参数仅用于兼容性。

类型: Any 默认值: ...

default_factory

用于生成默认值的可调用对象(callable)。

这不会影响 Path 参数,因为该值总是必需的。此参数仅用于兼容性。

类型: Callable[[], Any] | None 默认值: _Unset

alias

参数字段的别名。

这将用于提取数据以及生成的 OpenAPI 中。当您因为 Python 保留关键字等原因无法使用想要的名称时,它特别有用。

类型: str | None 默认值: None

alias_priority

别名的优先级。这会影响是否使用别名生成器。

类型: int | None 默认值: _Unset

validation_alias

“白名单”验证步骤。参数字段将是别名或定义的别名集中唯一允许的那个。

类型: str | AliasPath | AliasChoices | None 默认值: None

serialization_alias

“黑名单”验证步骤。原始参数字段将是别名或别名集字段中的唯一一个,所有其他字段在序列化时将被忽略。

类型: str | None 默认值: None

title

人类可读的标题。

FastAPI 关于路径参数和数值验证的文档 中了解更多信息。

类型: str | None 默认值: None

description

人类可读的描述。

类型: str | None 默认值: None

gt

大于。如果设置,值必须大于此值。仅适用于数字。

FastAPI 关于路径参数数值验证的文档 中了解更多信息。

类型: float | None 默认值: None

ge

大于或等于。如果设置,值必须大于或等于此值。仅适用于数字。

FastAPI 关于路径参数数值验证的文档 中了解更多信息。

类型: float | None 默认值: None

lt

小于。如果设置,值必须小于此值。仅适用于数字。

FastAPI 关于路径参数数值验证的文档 中了解更多信息。

类型: float | None 默认值: None

le

小于或等于。如果设置,值必须小于或等于此值。仅适用于数字。

FastAPI 关于路径参数数值验证的文档 中了解更多信息。

类型: float | None 默认值: None

min_length

字符串的最小长度。

类型: int | None 默认值: None

max_length

字符串的最大长度。

类型: int | None 默认值: None

pattern

字符串的正则表达式模式。

类型: str | None 默认值: None

regex

在 FastAPI 0.100.0 和 Pydantic v2 中已弃用,请改用 pattern。字符串的正则表达式模式。

类型: str | None 默认值: None

discriminator

用于在带标签的联合类型(tagged union)中区分类型的参数字段名称。

类型: str | None 默认值: None

strict

如果为 True,则对该字段应用严格验证。

类型: bool | None 默认值: _Unset

multiple_of

值必须是此数字的倍数。仅适用于数字。

类型: float | None 默认值: _Unset

allow_inf_nan

允许 inf-infnan。仅适用于数字。

类型: bool | None 默认值: _Unset

max_digits

十进制数值允许的最大位数。

类型: int | None 默认值: _Unset

decimal_places

十进制数值允许的小数位数。

类型: int | None 默认值: _Unset

examples

此字段的示例值。

FastAPI 关于声明请求示例数据的文档 中了解更多信息。

类型: list[Any] | None 默认值: None

example

在 OpenAPI 3.1.0 中已弃用(现已使用 JSON Schema 2020-12),尽管仍受支持。请改用 examples。

类型: Any | None 默认值: _Unset

openapi_examples

OpenAPI 特有的示例。

它将被添加到生成的 OpenAPI (例如在 /docs 中可见)。

Swagger UI(提供 /docs 界面)对 OpenAPI 特有示例的支持比对 JSON Schema examples 的支持更好,这是其主要应用场景。

FastAPI 关于声明请求示例数据的文档 中了解更多信息。

类型: dict[str, Example] | None 默认值: None

deprecated

将此参数字段标记为已弃用。

它会影响生成的 OpenAPI(例如在 /docs 中可见)。

类型: deprecated | str | bool | None 默认值: None

include_in_schema

用于决定是否在生成的 OpenAPI 中包含此参数字段。您可能不需要它,但它是可用的。

这会影响生成的 OpenAPI (例如在 /docs 中可见)。

类型: bool 默认值: True

json_schema_extra

任何额外的 JSON schema 数据。

类型: dict[str, Any] | None 默认值: None

**extra

extra 参数已弃用。请改用 json_schema_extra。包含 JSON Schema 使用的额外字段。

类型: Any 默认值: {}

源代码位于 fastapi/param_functions.py
def Path(  # noqa: N802
    default: Annotated[
        Any,
        Doc(
            """
            Default value if the parameter field is not set.

            This doesn't affect `Path` parameters as the value is always required.
            The parameter is available only for compatibility.
            """
        ),
    ] = ...,
    *,
    default_factory: Annotated[
        Callable[[], Any] | None,
        Doc(
            """
            A callable to generate the default value.

            This doesn't affect `Path` parameters as the value is always required.
            The parameter is available only for compatibility.
            """
        ),
    ] = _Unset,
    alias: Annotated[
        str | None,
        Doc(
            """
            An alternative name for the parameter field.

            This will be used to extract the data and for the generated OpenAPI.
            It is particularly useful when you can't use the name you want because it
            is a Python reserved keyword or similar.
            """
        ),
    ] = None,
    alias_priority: Annotated[
        int | None,
        Doc(
            """
            Priority of the alias. This affects whether an alias generator is used.
            """
        ),
    ] = _Unset,
    validation_alias: Annotated[
        str | AliasPath | AliasChoices | None,
        Doc(
            """
            'Whitelist' validation step. The parameter field will be the single one
            allowed by the alias or set of aliases defined.
            """
        ),
    ] = None,
    serialization_alias: Annotated[
        str | None,
        Doc(
            """
            'Blacklist' validation step. The vanilla parameter field will be the
            single one of the alias' or set of aliases' fields and all the other
            fields will be ignored at serialization time.
            """
        ),
    ] = None,
    title: Annotated[
        str | None,
        Doc(
            """
            Human-readable title.

            Read more about it in the
            [FastAPI docs for Path Parameters and Numeric Validations](https://fastapi.org.cn/tutorial/path-params-numeric-validations/#declare-metadata)
            """
        ),
    ] = None,
    description: Annotated[
        str | None,
        Doc(
            """
            Human-readable description.
            """
        ),
    ] = None,
    gt: Annotated[
        float | None,
        Doc(
            """
            Greater than. If set, value must be greater than this. Only applicable to
            numbers.

            Read more about it in the
            [FastAPI docs about Path parameters numeric validations](https://fastapi.org.cn/tutorial/path-params-numeric-validations/#number-validations-greater-than-and-less-than-or-equal)
            """
        ),
    ] = None,
    ge: Annotated[
        float | None,
        Doc(
            """
            Greater than or equal. If set, value must be greater than or equal to
            this. Only applicable to numbers.

            Read more about it in the
            [FastAPI docs about Path parameters numeric validations](https://fastapi.org.cn/tutorial/path-params-numeric-validations/#number-validations-greater-than-and-less-than-or-equal)
            """
        ),
    ] = None,
    lt: Annotated[
        float | None,
        Doc(
            """
            Less than. If set, value must be less than this. Only applicable to numbers.

            Read more about it in the
            [FastAPI docs about Path parameters numeric validations](https://fastapi.org.cn/tutorial/path-params-numeric-validations/#number-validations-greater-than-and-less-than-or-equal)
            """
        ),
    ] = None,
    le: Annotated[
        float | None,
        Doc(
            """
            Less than or equal. If set, value must be less than or equal to this.
            Only applicable to numbers.

            Read more about it in the
            [FastAPI docs about Path parameters numeric validations](https://fastapi.org.cn/tutorial/path-params-numeric-validations/#number-validations-greater-than-and-less-than-or-equal)
            """
        ),
    ] = None,
    min_length: Annotated[
        int | None,
        Doc(
            """
            Minimum length for strings.
            """
        ),
    ] = None,
    max_length: Annotated[
        int | None,
        Doc(
            """
            Maximum length for strings.
            """
        ),
    ] = None,
    pattern: Annotated[
        str | None,
        Doc(
            """
            RegEx pattern for strings.
            """
        ),
    ] = None,
    regex: Annotated[
        str | None,
        Doc(
            """
            RegEx pattern for strings.
            """
        ),
        deprecated(
            "Deprecated in FastAPI 0.100.0 and Pydantic v2, use `pattern` instead."
        ),
    ] = None,
    discriminator: Annotated[
        str | None,
        Doc(
            """
            Parameter field name for discriminating the type in a tagged union.
            """
        ),
    ] = None,
    strict: Annotated[
        bool | None,
        Doc(
            """
            If `True`, strict validation is applied to the field.
            """
        ),
    ] = _Unset,
    multiple_of: Annotated[
        float | None,
        Doc(
            """
            Value must be a multiple of this. Only applicable to numbers.
            """
        ),
    ] = _Unset,
    allow_inf_nan: Annotated[
        bool | None,
        Doc(
            """
            Allow `inf`, `-inf`, `nan`. Only applicable to numbers.
            """
        ),
    ] = _Unset,
    max_digits: Annotated[
        int | None,
        Doc(
            """
            Maximum number of digits allowed for decimal values.
            """
        ),
    ] = _Unset,
    decimal_places: Annotated[
        int | None,
        Doc(
            """
            Maximum number of decimal places allowed for decimal values.
            """
        ),
    ] = _Unset,
    examples: Annotated[
        list[Any] | None,
        Doc(
            """
            Example values for this field.

            Read more about it in the
            [FastAPI docs for Declare Request Example Data](https://fastapi.org.cn/tutorial/schema-extra-example/)
            """
        ),
    ] = None,
    example: Annotated[
        Any | None,
        deprecated(
            "Deprecated in OpenAPI 3.1.0 that now uses JSON Schema 2020-12, "
            "although still supported. Use examples instead."
        ),
    ] = _Unset,
    openapi_examples: Annotated[
        dict[str, Example] | None,
        Doc(
            """
            OpenAPI-specific examples.

            It will be added to the generated OpenAPI (e.g. visible at `/docs`).

            Swagger UI (that provides the `/docs` interface) has better support for the
            OpenAPI-specific examples than the JSON Schema `examples`, that's the main
            use case for this.

            Read more about it in the
            [FastAPI docs for Declare Request Example Data](https://fastapi.org.cn/tutorial/schema-extra-example/#using-the-openapi_examples-parameter).
            """
        ),
    ] = None,
    deprecated: Annotated[
        deprecated | str | bool | None,
        Doc(
            """
            Mark this parameter field as deprecated.

            It will affect the generated OpenAPI (e.g. visible at `/docs`).
            """
        ),
    ] = None,
    include_in_schema: Annotated[
        bool,
        Doc(
            """
            To include (or not) this parameter field in the generated OpenAPI.
            You probably don't need it, but it's available.

            This affects the generated OpenAPI (e.g. visible at `/docs`).
            """
        ),
    ] = True,
    json_schema_extra: Annotated[
        dict[str, Any] | None,
        Doc(
            """
            Any additional JSON schema data.
            """
        ),
    ] = None,
    **extra: Annotated[
        Any,
        Doc(
            """
            Include extra fields used by the JSON Schema.
            """
        ),
        deprecated(
            """
            The `extra` kwargs is deprecated. Use `json_schema_extra` instead.
            """
        ),
    ],
) -> Any:
    """
    Declare a path parameter for a *path operation*.

    Read more about it in the
    [FastAPI docs for Path Parameters and Numeric Validations](https://fastapi.org.cn/tutorial/path-params-numeric-validations/).

    ```python
    from typing import Annotated

    from fastapi import FastAPI, Path

    app = FastAPI()


    @app.get("/items/{item_id}")
    async def read_items(
        item_id: Annotated[int, Path(title="The ID of the item to get")],
    ):
        return {"item_id": item_id}
    ```
    """
    return params.Path(
        default=default,
        default_factory=default_factory,
        alias=alias,
        alias_priority=alias_priority,
        validation_alias=validation_alias,
        serialization_alias=serialization_alias,
        title=title,
        description=description,
        gt=gt,
        ge=ge,
        lt=lt,
        le=le,
        min_length=min_length,
        max_length=max_length,
        pattern=pattern,
        regex=regex,
        discriminator=discriminator,
        strict=strict,
        multiple_of=multiple_of,
        allow_inf_nan=allow_inf_nan,
        max_digits=max_digits,
        decimal_places=decimal_places,
        example=example,
        examples=examples,
        openapi_examples=openapi_examples,
        deprecated=deprecated,
        include_in_schema=include_in_schema,
        json_schema_extra=json_schema_extra,
        **extra,
    )

fastapi.Body

Body(
    default=Undefined,
    *,
    default_factory=_Unset,
    embed=None,
    media_type="application/json",
    alias=None,
    alias_priority=_Unset,
    validation_alias=None,
    serialization_alias=None,
    title=None,
    description=None,
    gt=None,
    ge=None,
    lt=None,
    le=None,
    min_length=None,
    max_length=None,
    pattern=None,
    regex=None,
    discriminator=None,
    strict=_Unset,
    multiple_of=_Unset,
    allow_inf_nan=_Unset,
    max_digits=_Unset,
    decimal_places=_Unset,
    examples=None,
    example=_Unset,
    openapi_examples=None,
    deprecated=None,
    include_in_schema=True,
    json_schema_extra=None,
    **extra
)
参数 描述
default

如果未设置参数字段,则使用的默认值。

类型: Any 默认值: Undefined

default_factory

用于生成默认值的可调用对象(callable)。

这不会影响 Path 参数,因为该值总是必需的。此参数仅用于兼容性。

类型: Callable[[], Any] | None 默认值: _Unset

embed

embedTrue 时,参数将在 JSON 正文中作为一个键(key)出现,而不是作为 JSON 正文本身。

当声明了多个 Body 参数时,这种情况会自动发生。

FastAPI 关于 Body - 多参数的文档 中了解更多信息。

类型: bool | None 默认值: None

media_type

此参数字段的媒体类型。更改它会影响生成的 OpenAPI,但目前不会影响数据的解析。

类型: str 默认值: 'application/json'

alias

参数字段的别名。

这将用于提取数据以及生成的 OpenAPI 中。当您因为 Python 保留关键字等原因无法使用想要的名称时,它特别有用。

类型: str | None 默认值: None

alias_priority

别名的优先级。这会影响是否使用别名生成器。

类型: int | None 默认值: _Unset

validation_alias

“白名单”验证步骤。参数字段将是别名或定义的别名集中唯一允许的那个。

类型: str | AliasPath | AliasChoices | None 默认值: None

serialization_alias

“黑名单”验证步骤。原始参数字段将是别名或别名集字段中的唯一一个,所有其他字段在序列化时将被忽略。

类型: str | None 默认值: None

title

人类可读的标题。

类型: str | None 默认值: None

description

人类可读的描述。

类型: str | None 默认值: None

gt

大于。如果设置,值必须大于此值。仅适用于数字。

类型: float | None 默认值: None

ge

大于或等于。如果设置,值必须大于或等于此值。仅适用于数字。

类型: float | None 默认值: None

lt

小于。如果设置,值必须小于此值。仅适用于数字。

类型: float | None 默认值: None

le

小于或等于。如果设置,值必须小于或等于此值。仅适用于数字。

类型: float | None 默认值: None

min_length

字符串的最小长度。

类型: int | None 默认值: None

max_length

字符串的最大长度。

类型: int | None 默认值: None

pattern

字符串的正则表达式模式。

类型: str | None 默认值: None

regex

在 FastAPI 0.100.0 和 Pydantic v2 中已弃用,请改用 pattern。字符串的正则表达式模式。

类型: str | None 默认值: None

discriminator

用于在带标签的联合类型(tagged union)中区分类型的参数字段名称。

类型: str | None 默认值: None

strict

如果为 True,则对该字段应用严格验证。

类型: bool | None 默认值: _Unset

multiple_of

值必须是此数字的倍数。仅适用于数字。

类型: float | None 默认值: _Unset

allow_inf_nan

允许 inf-infnan。仅适用于数字。

类型: bool | None 默认值: _Unset

max_digits

十进制数值允许的最大位数。

类型: int | None 默认值: _Unset

decimal_places

十进制数值允许的小数位数。

类型: int | None 默认值: _Unset

examples

此字段的示例值。

FastAPI 关于声明请求示例数据的文档 中了解更多信息。

类型: list[Any] | None 默认值: None

example

在 OpenAPI 3.1.0 中已弃用(现已使用 JSON Schema 2020-12),尽管仍受支持。请改用 examples。

类型: Any | None 默认值: _Unset

openapi_examples

OpenAPI 特有的示例。

它将被添加到生成的 OpenAPI (例如在 /docs 中可见)。

Swagger UI(提供 /docs 界面)对 OpenAPI 特有示例的支持比对 JSON Schema examples 的支持更好,这是其主要应用场景。

FastAPI 关于声明请求示例数据的文档 中了解更多信息。

类型: dict[str, Example] | None 默认值: None

deprecated

将此参数字段标记为已弃用。

它会影响生成的 OpenAPI(例如在 /docs 中可见)。

类型: deprecated | str | bool | None 默认值: None

include_in_schema

用于决定是否在生成的 OpenAPI 中包含此参数字段。您可能不需要它,但它是可用的。

这会影响生成的 OpenAPI (例如在 /docs 中可见)。

类型: bool 默认值: True

json_schema_extra

任何额外的 JSON schema 数据。

类型: dict[str, Any] | None 默认值: None

**extra

extra 参数已弃用。请改用 json_schema_extra。包含 JSON Schema 使用的额外字段。

类型: Any 默认值: {}

源代码位于 fastapi/param_functions.py
def Body(  # noqa: N802
    default: Annotated[
        Any,
        Doc(
            """
            Default value if the parameter field is not set.
            """
        ),
    ] = Undefined,
    *,
    default_factory: Annotated[
        Callable[[], Any] | None,
        Doc(
            """
            A callable to generate the default value.

            This doesn't affect `Path` parameters as the value is always required.
            The parameter is available only for compatibility.
            """
        ),
    ] = _Unset,
    embed: Annotated[
        bool | None,
        Doc(
            """
            When `embed` is `True`, the parameter will be expected in a JSON body as a
            key instead of being the JSON body itself.

            This happens automatically when more than one `Body` parameter is declared.

            Read more about it in the
            [FastAPI docs for Body - Multiple Parameters](https://fastapi.org.cn/tutorial/body-multiple-params/#embed-a-single-body-parameter).
            """
        ),
    ] = None,
    media_type: Annotated[
        str,
        Doc(
            """
            The media type of this parameter field. Changing it would affect the
            generated OpenAPI, but currently it doesn't affect the parsing of the data.
            """
        ),
    ] = "application/json",
    alias: Annotated[
        str | None,
        Doc(
            """
            An alternative name for the parameter field.

            This will be used to extract the data and for the generated OpenAPI.
            It is particularly useful when you can't use the name you want because it
            is a Python reserved keyword or similar.
            """
        ),
    ] = None,
    alias_priority: Annotated[
        int | None,
        Doc(
            """
            Priority of the alias. This affects whether an alias generator is used.
            """
        ),
    ] = _Unset,
    validation_alias: Annotated[
        str | AliasPath | AliasChoices | None,
        Doc(
            """
            'Whitelist' validation step. The parameter field will be the single one
            allowed by the alias or set of aliases defined.
            """
        ),
    ] = None,
    serialization_alias: Annotated[
        str | None,
        Doc(
            """
            'Blacklist' validation step. The vanilla parameter field will be the
            single one of the alias' or set of aliases' fields and all the other
            fields will be ignored at serialization time.
            """
        ),
    ] = None,
    title: Annotated[
        str | None,
        Doc(
            """
            Human-readable title.
            """
        ),
    ] = None,
    description: Annotated[
        str | None,
        Doc(
            """
            Human-readable description.
            """
        ),
    ] = None,
    gt: Annotated[
        float | None,
        Doc(
            """
            Greater than. If set, value must be greater than this. Only applicable to
            numbers.
            """
        ),
    ] = None,
    ge: Annotated[
        float | None,
        Doc(
            """
            Greater than or equal. If set, value must be greater than or equal to
            this. Only applicable to numbers.
            """
        ),
    ] = None,
    lt: Annotated[
        float | None,
        Doc(
            """
            Less than. If set, value must be less than this. Only applicable to numbers.
            """
        ),
    ] = None,
    le: Annotated[
        float | None,
        Doc(
            """
            Less than or equal. If set, value must be less than or equal to this.
            Only applicable to numbers.
            """
        ),
    ] = None,
    min_length: Annotated[
        int | None,
        Doc(
            """
            Minimum length for strings.
            """
        ),
    ] = None,
    max_length: Annotated[
        int | None,
        Doc(
            """
            Maximum length for strings.
            """
        ),
    ] = None,
    pattern: Annotated[
        str | None,
        Doc(
            """
            RegEx pattern for strings.
            """
        ),
    ] = None,
    regex: Annotated[
        str | None,
        Doc(
            """
            RegEx pattern for strings.
            """
        ),
        deprecated(
            "Deprecated in FastAPI 0.100.0 and Pydantic v2, use `pattern` instead."
        ),
    ] = None,
    discriminator: Annotated[
        str | None,
        Doc(
            """
            Parameter field name for discriminating the type in a tagged union.
            """
        ),
    ] = None,
    strict: Annotated[
        bool | None,
        Doc(
            """
            If `True`, strict validation is applied to the field.
            """
        ),
    ] = _Unset,
    multiple_of: Annotated[
        float | None,
        Doc(
            """
            Value must be a multiple of this. Only applicable to numbers.
            """
        ),
    ] = _Unset,
    allow_inf_nan: Annotated[
        bool | None,
        Doc(
            """
            Allow `inf`, `-inf`, `nan`. Only applicable to numbers.
            """
        ),
    ] = _Unset,
    max_digits: Annotated[
        int | None,
        Doc(
            """
            Maximum number of digits allowed for decimal values.
            """
        ),
    ] = _Unset,
    decimal_places: Annotated[
        int | None,
        Doc(
            """
            Maximum number of decimal places allowed for decimal values.
            """
        ),
    ] = _Unset,
    examples: Annotated[
        list[Any] | None,
        Doc(
            """
            Example values for this field.

            Read more about it in the
            [FastAPI docs for Declare Request Example Data](https://fastapi.org.cn/tutorial/schema-extra-example/)
            """
        ),
    ] = None,
    example: Annotated[
        Any | None,
        deprecated(
            "Deprecated in OpenAPI 3.1.0 that now uses JSON Schema 2020-12, "
            "although still supported. Use examples instead."
        ),
    ] = _Unset,
    openapi_examples: Annotated[
        dict[str, Example] | None,
        Doc(
            """
            OpenAPI-specific examples.

            It will be added to the generated OpenAPI (e.g. visible at `/docs`).

            Swagger UI (that provides the `/docs` interface) has better support for the
            OpenAPI-specific examples than the JSON Schema `examples`, that's the main
            use case for this.

            Read more about it in the
            [FastAPI docs for Declare Request Example Data](https://fastapi.org.cn/tutorial/schema-extra-example/#using-the-openapi_examples-parameter).
            """
        ),
    ] = None,
    deprecated: Annotated[
        deprecated | str | bool | None,
        Doc(
            """
            Mark this parameter field as deprecated.

            It will affect the generated OpenAPI (e.g. visible at `/docs`).
            """
        ),
    ] = None,
    include_in_schema: Annotated[
        bool,
        Doc(
            """
            To include (or not) this parameter field in the generated OpenAPI.
            You probably don't need it, but it's available.

            This affects the generated OpenAPI (e.g. visible at `/docs`).
            """
        ),
    ] = True,
    json_schema_extra: Annotated[
        dict[str, Any] | None,
        Doc(
            """
            Any additional JSON schema data.
            """
        ),
    ] = None,
    **extra: Annotated[
        Any,
        Doc(
            """
            Include extra fields used by the JSON Schema.
            """
        ),
        deprecated(
            """
            The `extra` kwargs is deprecated. Use `json_schema_extra` instead.
            """
        ),
    ],
) -> Any:
    return params.Body(
        default=default,
        default_factory=default_factory,
        embed=embed,
        media_type=media_type,
        alias=alias,
        alias_priority=alias_priority,
        validation_alias=validation_alias,
        serialization_alias=serialization_alias,
        title=title,
        description=description,
        gt=gt,
        ge=ge,
        lt=lt,
        le=le,
        min_length=min_length,
        max_length=max_length,
        pattern=pattern,
        regex=regex,
        discriminator=discriminator,
        strict=strict,
        multiple_of=multiple_of,
        allow_inf_nan=allow_inf_nan,
        max_digits=max_digits,
        decimal_places=decimal_places,
        example=example,
        examples=examples,
        openapi_examples=openapi_examples,
        deprecated=deprecated,
        include_in_schema=include_in_schema,
        json_schema_extra=json_schema_extra,
        **extra,
    )

fastapi.Cookie

Cookie(
    default=Undefined,
    *,
    default_factory=_Unset,
    alias=None,
    alias_priority=_Unset,
    validation_alias=None,
    serialization_alias=None,
    title=None,
    description=None,
    gt=None,
    ge=None,
    lt=None,
    le=None,
    min_length=None,
    max_length=None,
    pattern=None,
    regex=None,
    discriminator=None,
    strict=_Unset,
    multiple_of=_Unset,
    allow_inf_nan=_Unset,
    max_digits=_Unset,
    decimal_places=_Unset,
    examples=None,
    example=_Unset,
    openapi_examples=None,
    deprecated=None,
    include_in_schema=True,
    json_schema_extra=None,
    **extra
)
参数 描述
default

如果未设置参数字段,则使用的默认值。

类型: Any 默认值: Undefined

default_factory

用于生成默认值的可调用对象(callable)。

这不会影响 Path 参数,因为该值总是必需的。此参数仅用于兼容性。

类型: Callable[[], Any] | None 默认值: _Unset

alias

参数字段的别名。

这将用于提取数据以及生成的 OpenAPI 中。当您因为 Python 保留关键字等原因无法使用想要的名称时,它特别有用。

类型: str | None 默认值: None

alias_priority

别名的优先级。这会影响是否使用别名生成器。

类型: int | None 默认值: _Unset

validation_alias

“白名单”验证步骤。参数字段将是别名或定义的别名集中唯一允许的那个。

类型: str | AliasPath | AliasChoices | None 默认值: None

serialization_alias

“黑名单”验证步骤。原始参数字段将是别名或别名集字段中的唯一一个,所有其他字段在序列化时将被忽略。

类型: str | None 默认值: None

title

人类可读的标题。

类型: str | None 默认值: None

description

人类可读的描述。

类型: str | None 默认值: None

gt

大于。如果设置,值必须大于此值。仅适用于数字。

类型: float | None 默认值: None

ge

大于或等于。如果设置,值必须大于或等于此值。仅适用于数字。

类型: float | None 默认值: None

lt

小于。如果设置,值必须小于此值。仅适用于数字。

类型: float | None 默认值: None

le

小于或等于。如果设置,值必须小于或等于此值。仅适用于数字。

类型: float | None 默认值: None

min_length

字符串的最小长度。

类型: int | None 默认值: None

max_length

字符串的最大长度。

类型: int | None 默认值: None

pattern

字符串的正则表达式模式。

类型: str | None 默认值: None

regex

在 FastAPI 0.100.0 和 Pydantic v2 中已弃用,请改用 pattern。字符串的正则表达式模式。

类型: str | None 默认值: None

discriminator

用于在带标签的联合类型(tagged union)中区分类型的参数字段名称。

类型: str | None 默认值: None

strict

如果为 True,则对该字段应用严格验证。

类型: bool | None 默认值: _Unset

multiple_of

值必须是此数字的倍数。仅适用于数字。

类型: float | None 默认值: _Unset

allow_inf_nan

允许 inf-infnan。仅适用于数字。

类型: bool | None 默认值: _Unset

max_digits

十进制数值允许的最大位数。

类型: int | None 默认值: _Unset

decimal_places

十进制数值允许的小数位数。

类型: int | None 默认值: _Unset

examples

此字段的示例值。

FastAPI 关于声明请求示例数据的文档 中了解更多信息。

类型: list[Any] | None 默认值: None

example

在 OpenAPI 3.1.0 中已弃用(现已使用 JSON Schema 2020-12),尽管仍受支持。请改用 examples。

类型: Any | None 默认值: _Unset

openapi_examples

OpenAPI 特有的示例。

它将被添加到生成的 OpenAPI (例如在 /docs 中可见)。

Swagger UI(提供 /docs 界面)对 OpenAPI 特有示例的支持比对 JSON Schema examples 的支持更好,这是其主要应用场景。

FastAPI 关于声明请求示例数据的文档 中了解更多信息。

类型: dict[str, Example] | None 默认值: None

deprecated

将此参数字段标记为已弃用。

它会影响生成的 OpenAPI(例如在 /docs 中可见)。

类型: deprecated | str | bool | None 默认值: None

include_in_schema

用于决定是否在生成的 OpenAPI 中包含此参数字段。您可能不需要它,但它是可用的。

这会影响生成的 OpenAPI (例如在 /docs 中可见)。

类型: bool 默认值: True

json_schema_extra

任何额外的 JSON schema 数据。

类型: dict[str, Any] | None 默认值: None

**extra

extra 参数已弃用。请改用 json_schema_extra。包含 JSON Schema 使用的额外字段。

类型: Any 默认值: {}

源代码位于 fastapi/param_functions.py
def Cookie(  # noqa: N802
    default: Annotated[
        Any,
        Doc(
            """
            Default value if the parameter field is not set.
            """
        ),
    ] = Undefined,
    *,
    default_factory: Annotated[
        Callable[[], Any] | None,
        Doc(
            """
            A callable to generate the default value.

            This doesn't affect `Path` parameters as the value is always required.
            The parameter is available only for compatibility.
            """
        ),
    ] = _Unset,
    alias: Annotated[
        str | None,
        Doc(
            """
            An alternative name for the parameter field.

            This will be used to extract the data and for the generated OpenAPI.
            It is particularly useful when you can't use the name you want because it
            is a Python reserved keyword or similar.
            """
        ),
    ] = None,
    alias_priority: Annotated[
        int | None,
        Doc(
            """
            Priority of the alias. This affects whether an alias generator is used.
            """
        ),
    ] = _Unset,
    validation_alias: Annotated[
        str | AliasPath | AliasChoices | None,
        Doc(
            """
            'Whitelist' validation step. The parameter field will be the single one
            allowed by the alias or set of aliases defined.
            """
        ),
    ] = None,
    serialization_alias: Annotated[
        str | None,
        Doc(
            """
            'Blacklist' validation step. The vanilla parameter field will be the
            single one of the alias' or set of aliases' fields and all the other
            fields will be ignored at serialization time.
            """
        ),
    ] = None,
    title: Annotated[
        str | None,
        Doc(
            """
            Human-readable title.
            """
        ),
    ] = None,
    description: Annotated[
        str | None,
        Doc(
            """
            Human-readable description.
            """
        ),
    ] = None,
    gt: Annotated[
        float | None,
        Doc(
            """
            Greater than. If set, value must be greater than this. Only applicable to
            numbers.
            """
        ),
    ] = None,
    ge: Annotated[
        float | None,
        Doc(
            """
            Greater than or equal. If set, value must be greater than or equal to
            this. Only applicable to numbers.
            """
        ),
    ] = None,
    lt: Annotated[
        float | None,
        Doc(
            """
            Less than. If set, value must be less than this. Only applicable to numbers.
            """
        ),
    ] = None,
    le: Annotated[
        float | None,
        Doc(
            """
            Less than or equal. If set, value must be less than or equal to this.
            Only applicable to numbers.
            """
        ),
    ] = None,
    min_length: Annotated[
        int | None,
        Doc(
            """
            Minimum length for strings.
            """
        ),
    ] = None,
    max_length: Annotated[
        int | None,
        Doc(
            """
            Maximum length for strings.
            """
        ),
    ] = None,
    pattern: Annotated[
        str | None,
        Doc(
            """
            RegEx pattern for strings.
            """
        ),
    ] = None,
    regex: Annotated[
        str | None,
        Doc(
            """
            RegEx pattern for strings.
            """
        ),
        deprecated(
            "Deprecated in FastAPI 0.100.0 and Pydantic v2, use `pattern` instead."
        ),
    ] = None,
    discriminator: Annotated[
        str | None,
        Doc(
            """
            Parameter field name for discriminating the type in a tagged union.
            """
        ),
    ] = None,
    strict: Annotated[
        bool | None,
        Doc(
            """
            If `True`, strict validation is applied to the field.
            """
        ),
    ] = _Unset,
    multiple_of: Annotated[
        float | None,
        Doc(
            """
            Value must be a multiple of this. Only applicable to numbers.
            """
        ),
    ] = _Unset,
    allow_inf_nan: Annotated[
        bool | None,
        Doc(
            """
            Allow `inf`, `-inf`, `nan`. Only applicable to numbers.
            """
        ),
    ] = _Unset,
    max_digits: Annotated[
        int | None,
        Doc(
            """
            Maximum number of digits allowed for decimal values.
            """
        ),
    ] = _Unset,
    decimal_places: Annotated[
        int | None,
        Doc(
            """
            Maximum number of decimal places allowed for decimal values.
            """
        ),
    ] = _Unset,
    examples: Annotated[
        list[Any] | None,
        Doc(
            """
            Example values for this field.

            Read more about it in the
            [FastAPI docs for Declare Request Example Data](https://fastapi.org.cn/tutorial/schema-extra-example/)
            """
        ),
    ] = None,
    example: Annotated[
        Any | None,
        deprecated(
            "Deprecated in OpenAPI 3.1.0 that now uses JSON Schema 2020-12, "
            "although still supported. Use examples instead."
        ),
    ] = _Unset,
    openapi_examples: Annotated[
        dict[str, Example] | None,
        Doc(
            """
            OpenAPI-specific examples.

            It will be added to the generated OpenAPI (e.g. visible at `/docs`).

            Swagger UI (that provides the `/docs` interface) has better support for the
            OpenAPI-specific examples than the JSON Schema `examples`, that's the main
            use case for this.

            Read more about it in the
            [FastAPI docs for Declare Request Example Data](https://fastapi.org.cn/tutorial/schema-extra-example/#using-the-openapi_examples-parameter).
            """
        ),
    ] = None,
    deprecated: Annotated[
        deprecated | str | bool | None,
        Doc(
            """
            Mark this parameter field as deprecated.

            It will affect the generated OpenAPI (e.g. visible at `/docs`).
            """
        ),
    ] = None,
    include_in_schema: Annotated[
        bool,
        Doc(
            """
            To include (or not) this parameter field in the generated OpenAPI.
            You probably don't need it, but it's available.

            This affects the generated OpenAPI (e.g. visible at `/docs`).
            """
        ),
    ] = True,
    json_schema_extra: Annotated[
        dict[str, Any] | None,
        Doc(
            """
            Any additional JSON schema data.
            """
        ),
    ] = None,
    **extra: Annotated[
        Any,
        Doc(
            """
            Include extra fields used by the JSON Schema.
            """
        ),
        deprecated(
            """
            The `extra` kwargs is deprecated. Use `json_schema_extra` instead.
            """
        ),
    ],
) -> Any:
    return params.Cookie(
        default=default,
        default_factory=default_factory,
        alias=alias,
        alias_priority=alias_priority,
        validation_alias=validation_alias,
        serialization_alias=serialization_alias,
        title=title,
        description=description,
        gt=gt,
        ge=ge,
        lt=lt,
        le=le,
        min_length=min_length,
        max_length=max_length,
        pattern=pattern,
        regex=regex,
        discriminator=discriminator,
        strict=strict,
        multiple_of=multiple_of,
        allow_inf_nan=allow_inf_nan,
        max_digits=max_digits,
        decimal_places=decimal_places,
        example=example,
        examples=examples,
        openapi_examples=openapi_examples,
        deprecated=deprecated,
        include_in_schema=include_in_schema,
        json_schema_extra=json_schema_extra,
        **extra,
    )

fastapi.Header

Header(
    default=Undefined,
    *,
    default_factory=_Unset,
    alias=None,
    alias_priority=_Unset,
    validation_alias=None,
    serialization_alias=None,
    convert_underscores=True,
    title=None,
    description=None,
    gt=None,
    ge=None,
    lt=None,
    le=None,
    min_length=None,
    max_length=None,
    pattern=None,
    regex=None,
    discriminator=None,
    strict=_Unset,
    multiple_of=_Unset,
    allow_inf_nan=_Unset,
    max_digits=_Unset,
    decimal_places=_Unset,
    examples=None,
    example=_Unset,
    openapi_examples=None,
    deprecated=None,
    include_in_schema=True,
    json_schema_extra=None,
    **extra
)
参数 描述
default

如果未设置参数字段,则使用的默认值。

类型: Any 默认值: Undefined

default_factory

用于生成默认值的可调用对象(callable)。

这不会影响 Path 参数,因为该值总是必需的。此参数仅用于兼容性。

类型: Callable[[], Any] | None 默认值: _Unset

alias

参数字段的别名。

这将用于提取数据以及生成的 OpenAPI 中。当您因为 Python 保留关键字等原因无法使用想要的名称时,它特别有用。

类型: str | None 默认值: None

alias_priority

别名的优先级。这会影响是否使用别名生成器。

类型: int | None 默认值: _Unset

validation_alias

“白名单”验证步骤。参数字段将是别名或定义的别名集中唯一允许的那个。

类型: str | AliasPath | AliasChoices | None 默认值: None

serialization_alias

“黑名单”验证步骤。原始参数字段将是别名或别名集字段中的唯一一个,所有其他字段在序列化时将被忽略。

类型: str | None 默认值: None

convert_underscores

自动将参数字段名称中的下划线转换为连字符。

FastAPI 关于请求头参数的文档 中了解更多信息。

类型: bool 默认值: True

title

人类可读的标题。

类型: str | None 默认值: None

description

人类可读的描述。

类型: str | None 默认值: None

gt

大于。如果设置,值必须大于此值。仅适用于数字。

类型: float | None 默认值: None

ge

大于或等于。如果设置,值必须大于或等于此值。仅适用于数字。

类型: float | None 默认值: None

lt

小于。如果设置,值必须小于此值。仅适用于数字。

类型: float | None 默认值: None

le

小于或等于。如果设置,值必须小于或等于此值。仅适用于数字。

类型: float | None 默认值: None

min_length

字符串的最小长度。

类型: int | None 默认值: None

max_length

字符串的最大长度。

类型: int | None 默认值: None

pattern

字符串的正则表达式模式。

类型: str | None 默认值: None

regex

在 FastAPI 0.100.0 和 Pydantic v2 中已弃用,请改用 pattern。字符串的正则表达式模式。

类型: str | None 默认值: None

discriminator

用于在带标签的联合类型(tagged union)中区分类型的参数字段名称。

类型: str | None 默认值: None

strict

如果为 True,则对该字段应用严格验证。

类型: bool | None 默认值: _Unset

multiple_of

值必须是此数字的倍数。仅适用于数字。

类型: float | None 默认值: _Unset

allow_inf_nan

允许 inf-infnan。仅适用于数字。

类型: bool | None 默认值: _Unset

max_digits

十进制数值允许的最大位数。

类型: int | None 默认值: _Unset

decimal_places

十进制数值允许的小数位数。

类型: int | None 默认值: _Unset

examples

此字段的示例值。

FastAPI 关于声明请求示例数据的文档 中了解更多信息。

类型: list[Any] | None 默认值: None

example

在 OpenAPI 3.1.0 中已弃用(现已使用 JSON Schema 2020-12),尽管仍受支持。请改用 examples。

类型: Any | None 默认值: _Unset

openapi_examples

OpenAPI 特有的示例。

它将被添加到生成的 OpenAPI (例如在 /docs 中可见)。

Swagger UI(提供 /docs 界面)对 OpenAPI 特有示例的支持比对 JSON Schema examples 的支持更好,这是其主要应用场景。

FastAPI 关于声明请求示例数据的文档 中了解更多信息。

类型: dict[str, Example] | None 默认值: None

deprecated

将此参数字段标记为已弃用。

它会影响生成的 OpenAPI(例如在 /docs 中可见)。

类型: deprecated | str | bool | None 默认值: None

include_in_schema

用于决定是否在生成的 OpenAPI 中包含此参数字段。您可能不需要它,但它是可用的。

这会影响生成的 OpenAPI (例如在 /docs 中可见)。

类型: bool 默认值: True

json_schema_extra

任何额外的 JSON schema 数据。

类型: dict[str, Any] | None 默认值: None

**extra

extra 参数已弃用。请改用 json_schema_extra。包含 JSON Schema 使用的额外字段。

类型: Any 默认值: {}

源代码位于 fastapi/param_functions.py
def Header(  # noqa: N802
    default: Annotated[
        Any,
        Doc(
            """
            Default value if the parameter field is not set.
            """
        ),
    ] = Undefined,
    *,
    default_factory: Annotated[
        Callable[[], Any] | None,
        Doc(
            """
            A callable to generate the default value.

            This doesn't affect `Path` parameters as the value is always required.
            The parameter is available only for compatibility.
            """
        ),
    ] = _Unset,
    alias: Annotated[
        str | None,
        Doc(
            """
            An alternative name for the parameter field.

            This will be used to extract the data and for the generated OpenAPI.
            It is particularly useful when you can't use the name you want because it
            is a Python reserved keyword or similar.
            """
        ),
    ] = None,
    alias_priority: Annotated[
        int | None,
        Doc(
            """
            Priority of the alias. This affects whether an alias generator is used.
            """
        ),
    ] = _Unset,
    validation_alias: Annotated[
        str | AliasPath | AliasChoices | None,
        Doc(
            """
            'Whitelist' validation step. The parameter field will be the single one
            allowed by the alias or set of aliases defined.
            """
        ),
    ] = None,
    serialization_alias: Annotated[
        str | None,
        Doc(
            """
            'Blacklist' validation step. The vanilla parameter field will be the
            single one of the alias' or set of aliases' fields and all the other
            fields will be ignored at serialization time.
            """
        ),
    ] = None,
    convert_underscores: Annotated[
        bool,
        Doc(
            """
            Automatically convert underscores to hyphens in the parameter field name.

            Read more about it in the
            [FastAPI docs for Header Parameters](https://fastapi.org.cn/tutorial/header-params/#automatic-conversion)
            """
        ),
    ] = True,
    title: Annotated[
        str | None,
        Doc(
            """
            Human-readable title.
            """
        ),
    ] = None,
    description: Annotated[
        str | None,
        Doc(
            """
            Human-readable description.
            """
        ),
    ] = None,
    gt: Annotated[
        float | None,
        Doc(
            """
            Greater than. If set, value must be greater than this. Only applicable to
            numbers.
            """
        ),
    ] = None,
    ge: Annotated[
        float | None,
        Doc(
            """
            Greater than or equal. If set, value must be greater than or equal to
            this. Only applicable to numbers.
            """
        ),
    ] = None,
    lt: Annotated[
        float | None,
        Doc(
            """
            Less than. If set, value must be less than this. Only applicable to numbers.
            """
        ),
    ] = None,
    le: Annotated[
        float | None,
        Doc(
            """
            Less than or equal. If set, value must be less than or equal to this.
            Only applicable to numbers.
            """
        ),
    ] = None,
    min_length: Annotated[
        int | None,
        Doc(
            """
            Minimum length for strings.
            """
        ),
    ] = None,
    max_length: Annotated[
        int | None,
        Doc(
            """
            Maximum length for strings.
            """
        ),
    ] = None,
    pattern: Annotated[
        str | None,
        Doc(
            """
            RegEx pattern for strings.
            """
        ),
    ] = None,
    regex: Annotated[
        str | None,
        Doc(
            """
            RegEx pattern for strings.
            """
        ),
        deprecated(
            "Deprecated in FastAPI 0.100.0 and Pydantic v2, use `pattern` instead."
        ),
    ] = None,
    discriminator: Annotated[
        str | None,
        Doc(
            """
            Parameter field name for discriminating the type in a tagged union.
            """
        ),
    ] = None,
    strict: Annotated[
        bool | None,
        Doc(
            """
            If `True`, strict validation is applied to the field.
            """
        ),
    ] = _Unset,
    multiple_of: Annotated[
        float | None,
        Doc(
            """
            Value must be a multiple of this. Only applicable to numbers.
            """
        ),
    ] = _Unset,
    allow_inf_nan: Annotated[
        bool | None,
        Doc(
            """
            Allow `inf`, `-inf`, `nan`. Only applicable to numbers.
            """
        ),
    ] = _Unset,
    max_digits: Annotated[
        int | None,
        Doc(
            """
            Maximum number of digits allowed for decimal values.
            """
        ),
    ] = _Unset,
    decimal_places: Annotated[
        int | None,
        Doc(
            """
            Maximum number of decimal places allowed for decimal values.
            """
        ),
    ] = _Unset,
    examples: Annotated[
        list[Any] | None,
        Doc(
            """
            Example values for this field.

            Read more about it in the
            [FastAPI docs for Declare Request Example Data](https://fastapi.org.cn/tutorial/schema-extra-example/)
            """
        ),
    ] = None,
    example: Annotated[
        Any | None,
        deprecated(
            "Deprecated in OpenAPI 3.1.0 that now uses JSON Schema 2020-12, "
            "although still supported. Use examples instead."
        ),
    ] = _Unset,
    openapi_examples: Annotated[
        dict[str, Example] | None,
        Doc(
            """
            OpenAPI-specific examples.

            It will be added to the generated OpenAPI (e.g. visible at `/docs`).

            Swagger UI (that provides the `/docs` interface) has better support for the
            OpenAPI-specific examples than the JSON Schema `examples`, that's the main
            use case for this.

            Read more about it in the
            [FastAPI docs for Declare Request Example Data](https://fastapi.org.cn/tutorial/schema-extra-example/#using-the-openapi_examples-parameter).
            """
        ),
    ] = None,
    deprecated: Annotated[
        deprecated | str | bool | None,
        Doc(
            """
            Mark this parameter field as deprecated.

            It will affect the generated OpenAPI (e.g. visible at `/docs`).
            """
        ),
    ] = None,
    include_in_schema: Annotated[
        bool,
        Doc(
            """
            To include (or not) this parameter field in the generated OpenAPI.
            You probably don't need it, but it's available.

            This affects the generated OpenAPI (e.g. visible at `/docs`).
            """
        ),
    ] = True,
    json_schema_extra: Annotated[
        dict[str, Any] | None,
        Doc(
            """
            Any additional JSON schema data.
            """
        ),
    ] = None,
    **extra: Annotated[
        Any,
        Doc(
            """
            Include extra fields used by the JSON Schema.
            """
        ),
        deprecated(
            """
            The `extra` kwargs is deprecated. Use `json_schema_extra` instead.
            """
        ),
    ],
) -> Any:
    return params.Header(
        default=default,
        default_factory=default_factory,
        alias=alias,
        alias_priority=alias_priority,
        validation_alias=validation_alias,
        serialization_alias=serialization_alias,
        convert_underscores=convert_underscores,
        title=title,
        description=description,
        gt=gt,
        ge=ge,
        lt=lt,
        le=le,
        min_length=min_length,
        max_length=max_length,
        pattern=pattern,
        regex=regex,
        discriminator=discriminator,
        strict=strict,
        multiple_of=multiple_of,
        allow_inf_nan=allow_inf_nan,
        max_digits=max_digits,
        decimal_places=decimal_places,
        example=example,
        examples=examples,
        openapi_examples=openapi_examples,
        deprecated=deprecated,
        include_in_schema=include_in_schema,
        json_schema_extra=json_schema_extra,
        **extra,
    )

fastapi.Form

Form(
    default=Undefined,
    *,
    default_factory=_Unset,
    media_type="application/x-www-form-urlencoded",
    alias=None,
    alias_priority=_Unset,
    validation_alias=None,
    serialization_alias=None,
    title=None,
    description=None,
    gt=None,
    ge=None,
    lt=None,
    le=None,
    min_length=None,
    max_length=None,
    pattern=None,
    regex=None,
    discriminator=None,
    strict=_Unset,
    multiple_of=_Unset,
    allow_inf_nan=_Unset,
    max_digits=_Unset,
    decimal_places=_Unset,
    examples=None,
    example=_Unset,
    openapi_examples=None,
    deprecated=None,
    include_in_schema=True,
    json_schema_extra=None,
    **extra
)
参数 描述
default

如果未设置参数字段,则使用的默认值。

类型: Any 默认值: Undefined

default_factory

用于生成默认值的可调用对象(callable)。

这不会影响 Path 参数,因为该值总是必需的。此参数仅用于兼容性。

类型: Callable[[], Any] | None 默认值: _Unset

media_type

此参数字段的媒体类型。更改它会影响生成的 OpenAPI,但目前不会影响数据的解析。

类型: str 默认值: 'application/x-www-form-urlencoded'

alias

参数字段的别名。

这将用于提取数据以及生成的 OpenAPI 中。当您因为 Python 保留关键字等原因无法使用想要的名称时,它特别有用。

类型: str | None 默认值: None

alias_priority

别名的优先级。这会影响是否使用别名生成器。

类型: int | None 默认值: _Unset

validation_alias

“白名单”验证步骤。参数字段将是别名或定义的别名集中唯一允许的那个。

类型: str | AliasPath | AliasChoices | None 默认值: None

serialization_alias

“黑名单”验证步骤。原始参数字段将是别名或别名集字段中的唯一一个,所有其他字段在序列化时将被忽略。

类型: str | None 默认值: None

title

人类可读的标题。

类型: str | None 默认值: None

description

人类可读的描述。

类型: str | None 默认值: None

gt

大于。如果设置,值必须大于此值。仅适用于数字。

类型: float | None 默认值: None

ge

大于或等于。如果设置,值必须大于或等于此值。仅适用于数字。

类型: float | None 默认值: None

lt

小于。如果设置,值必须小于此值。仅适用于数字。

类型: float | None 默认值: None

le

小于或等于。如果设置,值必须小于或等于此值。仅适用于数字。

类型: float | None 默认值: None

min_length

字符串的最小长度。

类型: int | None 默认值: None

max_length

字符串的最大长度。

类型: int | None 默认值: None

pattern

字符串的正则表达式模式。

类型: str | None 默认值: None

regex

在 FastAPI 0.100.0 和 Pydantic v2 中已弃用,请改用 pattern。字符串的正则表达式模式。

类型: str | None 默认值: None

discriminator

用于在带标签的联合类型(tagged union)中区分类型的参数字段名称。

类型: str | None 默认值: None

strict

如果为 True,则对该字段应用严格验证。

类型: bool | None 默认值: _Unset

multiple_of

值必须是此数字的倍数。仅适用于数字。

类型: float | None 默认值: _Unset

allow_inf_nan

允许 inf-infnan。仅适用于数字。

类型: bool | None 默认值: _Unset

max_digits

十进制数值允许的最大位数。

类型: int | None 默认值: _Unset

decimal_places

十进制数值允许的小数位数。

类型: int | None 默认值: _Unset

examples

此字段的示例值。

FastAPI 关于声明请求示例数据的文档 中了解更多信息。

类型: list[Any] | None 默认值: None

example

在 OpenAPI 3.1.0 中已弃用(现已使用 JSON Schema 2020-12),尽管仍受支持。请改用 examples。

类型: Any | None 默认值: _Unset

openapi_examples

OpenAPI 特有的示例。

它将被添加到生成的 OpenAPI (例如在 /docs 中可见)。

Swagger UI(提供 /docs 界面)对 OpenAPI 特有示例的支持比对 JSON Schema examples 的支持更好,这是其主要应用场景。

FastAPI 关于声明请求示例数据的文档 中了解更多信息。

类型: dict[str, Example] | None 默认值: None

deprecated

将此参数字段标记为已弃用。

它会影响生成的 OpenAPI(例如在 /docs 中可见)。

类型: deprecated | str | bool | None 默认值: None

include_in_schema

用于决定是否在生成的 OpenAPI 中包含此参数字段。您可能不需要它,但它是可用的。

这会影响生成的 OpenAPI (例如在 /docs 中可见)。

类型: bool 默认值: True

json_schema_extra

任何额外的 JSON schema 数据。

类型: dict[str, Any] | None 默认值: None

**extra

extra 参数已弃用。请改用 json_schema_extra。包含 JSON Schema 使用的额外字段。

类型: Any 默认值: {}

源代码位于 fastapi/param_functions.py
def Form(  # noqa: N802
    default: Annotated[
        Any,
        Doc(
            """
            Default value if the parameter field is not set.
            """
        ),
    ] = Undefined,
    *,
    default_factory: Annotated[
        Callable[[], Any] | None,
        Doc(
            """
            A callable to generate the default value.

            This doesn't affect `Path` parameters as the value is always required.
            The parameter is available only for compatibility.
            """
        ),
    ] = _Unset,
    media_type: Annotated[
        str,
        Doc(
            """
            The media type of this parameter field. Changing it would affect the
            generated OpenAPI, but currently it doesn't affect the parsing of the data.
            """
        ),
    ] = "application/x-www-form-urlencoded",
    alias: Annotated[
        str | None,
        Doc(
            """
            An alternative name for the parameter field.

            This will be used to extract the data and for the generated OpenAPI.
            It is particularly useful when you can't use the name you want because it
            is a Python reserved keyword or similar.
            """
        ),
    ] = None,
    alias_priority: Annotated[
        int | None,
        Doc(
            """
            Priority of the alias. This affects whether an alias generator is used.
            """
        ),
    ] = _Unset,
    validation_alias: Annotated[
        str | AliasPath | AliasChoices | None,
        Doc(
            """
            'Whitelist' validation step. The parameter field will be the single one
            allowed by the alias or set of aliases defined.
            """
        ),
    ] = None,
    serialization_alias: Annotated[
        str | None,
        Doc(
            """
            'Blacklist' validation step. The vanilla parameter field will be the
            single one of the alias' or set of aliases' fields and all the other
            fields will be ignored at serialization time.
            """
        ),
    ] = None,
    title: Annotated[
        str | None,
        Doc(
            """
            Human-readable title.
            """
        ),
    ] = None,
    description: Annotated[
        str | None,
        Doc(
            """
            Human-readable description.
            """
        ),
    ] = None,
    gt: Annotated[
        float | None,
        Doc(
            """
            Greater than. If set, value must be greater than this. Only applicable to
            numbers.
            """
        ),
    ] = None,
    ge: Annotated[
        float | None,
        Doc(
            """
            Greater than or equal. If set, value must be greater than or equal to
            this. Only applicable to numbers.
            """
        ),
    ] = None,
    lt: Annotated[
        float | None,
        Doc(
            """
            Less than. If set, value must be less than this. Only applicable to numbers.
            """
        ),
    ] = None,
    le: Annotated[
        float | None,
        Doc(
            """
            Less than or equal. If set, value must be less than or equal to this.
            Only applicable to numbers.
            """
        ),
    ] = None,
    min_length: Annotated[
        int | None,
        Doc(
            """
            Minimum length for strings.
            """
        ),
    ] = None,
    max_length: Annotated[
        int | None,
        Doc(
            """
            Maximum length for strings.
            """
        ),
    ] = None,
    pattern: Annotated[
        str | None,
        Doc(
            """
            RegEx pattern for strings.
            """
        ),
    ] = None,
    regex: Annotated[
        str | None,
        Doc(
            """
            RegEx pattern for strings.
            """
        ),
        deprecated(
            "Deprecated in FastAPI 0.100.0 and Pydantic v2, use `pattern` instead."
        ),
    ] = None,
    discriminator: Annotated[
        str | None,
        Doc(
            """
            Parameter field name for discriminating the type in a tagged union.
            """
        ),
    ] = None,
    strict: Annotated[
        bool | None,
        Doc(
            """
            If `True`, strict validation is applied to the field.
            """
        ),
    ] = _Unset,
    multiple_of: Annotated[
        float | None,
        Doc(
            """
            Value must be a multiple of this. Only applicable to numbers.
            """
        ),
    ] = _Unset,
    allow_inf_nan: Annotated[
        bool | None,
        Doc(
            """
            Allow `inf`, `-inf`, `nan`. Only applicable to numbers.
            """
        ),
    ] = _Unset,
    max_digits: Annotated[
        int | None,
        Doc(
            """
            Maximum number of digits allowed for decimal values.
            """
        ),
    ] = _Unset,
    decimal_places: Annotated[
        int | None,
        Doc(
            """
            Maximum number of decimal places allowed for decimal values.
            """
        ),
    ] = _Unset,
    examples: Annotated[
        list[Any] | None,
        Doc(
            """
            Example values for this field.

            Read more about it in the
            [FastAPI docs for Declare Request Example Data](https://fastapi.org.cn/tutorial/schema-extra-example/)
            """
        ),
    ] = None,
    example: Annotated[
        Any | None,
        deprecated(
            "Deprecated in OpenAPI 3.1.0 that now uses JSON Schema 2020-12, "
            "although still supported. Use examples instead."
        ),
    ] = _Unset,
    openapi_examples: Annotated[
        dict[str, Example] | None,
        Doc(
            """
            OpenAPI-specific examples.

            It will be added to the generated OpenAPI (e.g. visible at `/docs`).

            Swagger UI (that provides the `/docs` interface) has better support for the
            OpenAPI-specific examples than the JSON Schema `examples`, that's the main
            use case for this.

            Read more about it in the
            [FastAPI docs for Declare Request Example Data](https://fastapi.org.cn/tutorial/schema-extra-example/#using-the-openapi_examples-parameter).
            """
        ),
    ] = None,
    deprecated: Annotated[
        deprecated | str | bool | None,
        Doc(
            """
            Mark this parameter field as deprecated.

            It will affect the generated OpenAPI (e.g. visible at `/docs`).
            """
        ),
    ] = None,
    include_in_schema: Annotated[
        bool,
        Doc(
            """
            To include (or not) this parameter field in the generated OpenAPI.
            You probably don't need it, but it's available.

            This affects the generated OpenAPI (e.g. visible at `/docs`).
            """
        ),
    ] = True,
    json_schema_extra: Annotated[
        dict[str, Any] | None,
        Doc(
            """
            Any additional JSON schema data.
            """
        ),
    ] = None,
    **extra: Annotated[
        Any,
        Doc(
            """
            Include extra fields used by the JSON Schema.
            """
        ),
        deprecated(
            """
            The `extra` kwargs is deprecated. Use `json_schema_extra` instead.
            """
        ),
    ],
) -> Any:
    return params.Form(
        default=default,
        default_factory=default_factory,
        media_type=media_type,
        alias=alias,
        alias_priority=alias_priority,
        validation_alias=validation_alias,
        serialization_alias=serialization_alias,
        title=title,
        description=description,
        gt=gt,
        ge=ge,
        lt=lt,
        le=le,
        min_length=min_length,
        max_length=max_length,
        pattern=pattern,
        regex=regex,
        discriminator=discriminator,
        strict=strict,
        multiple_of=multiple_of,
        allow_inf_nan=allow_inf_nan,
        max_digits=max_digits,
        decimal_places=decimal_places,
        example=example,
        examples=examples,
        openapi_examples=openapi_examples,
        deprecated=deprecated,
        include_in_schema=include_in_schema,
        json_schema_extra=json_schema_extra,
        **extra,
    )

fastapi.File

File(
    default=Undefined,
    *,
    default_factory=_Unset,
    media_type="multipart/form-data",
    alias=None,
    alias_priority=_Unset,
    validation_alias=None,
    serialization_alias=None,
    title=None,
    description=None,
    gt=None,
    ge=None,
    lt=None,
    le=None,
    min_length=None,
    max_length=None,
    pattern=None,
    regex=None,
    discriminator=None,
    strict=_Unset,
    multiple_of=_Unset,
    allow_inf_nan=_Unset,
    max_digits=_Unset,
    decimal_places=_Unset,
    examples=None,
    example=_Unset,
    openapi_examples=None,
    deprecated=None,
    include_in_schema=True,
    json_schema_extra=None,
    **extra
)
参数 描述
default

如果未设置参数字段,则使用的默认值。

类型: Any 默认值: Undefined

default_factory

用于生成默认值的可调用对象(callable)。

这不会影响 Path 参数,因为该值总是必需的。此参数仅用于兼容性。

类型: Callable[[], Any] | None 默认值: _Unset

media_type

此参数字段的媒体类型。更改它会影响生成的 OpenAPI,但目前不会影响数据的解析。

类型: str 默认值: 'multipart/form-data'

alias

参数字段的别名。

这将用于提取数据以及生成的 OpenAPI 中。当您因为 Python 保留关键字等原因无法使用想要的名称时,它特别有用。

类型: str | None 默认值: None

alias_priority

别名的优先级。这会影响是否使用别名生成器。

类型: int | None 默认值: _Unset

validation_alias

“白名单”验证步骤。参数字段将是别名或定义的别名集中唯一允许的那个。

类型: str | AliasPath | AliasChoices | None 默认值: None

serialization_alias

“黑名单”验证步骤。原始参数字段将是别名或别名集字段中的唯一一个,所有其他字段在序列化时将被忽略。

类型: str | None 默认值: None

title

人类可读的标题。

类型: str | None 默认值: None

description

人类可读的描述。

类型: str | None 默认值: None

gt

大于。如果设置,值必须大于此值。仅适用于数字。

类型: float | None 默认值: None

ge

大于或等于。如果设置,值必须大于或等于此值。仅适用于数字。

类型: float | None 默认值: None

lt

小于。如果设置,值必须小于此值。仅适用于数字。

类型: float | None 默认值: None

le

小于或等于。如果设置,值必须小于或等于此值。仅适用于数字。

类型: float | None 默认值: None

min_length

字符串的最小长度。

类型: int | None 默认值: None

max_length

字符串的最大长度。

类型: int | None 默认值: None

pattern

字符串的正则表达式模式。

类型: str | None 默认值: None

regex

在 FastAPI 0.100.0 和 Pydantic v2 中已弃用,请改用 pattern。字符串的正则表达式模式。

类型: str | None 默认值: None

discriminator

用于在带标签的联合类型(tagged union)中区分类型的参数字段名称。

类型: str | None 默认值: None

strict

如果为 True,则对该字段应用严格验证。

类型: bool | None 默认值: _Unset

multiple_of

值必须是此数字的倍数。仅适用于数字。

类型: float | None 默认值: _Unset

allow_inf_nan

允许 inf-infnan。仅适用于数字。

类型: bool | None 默认值: _Unset

max_digits

十进制数值允许的最大位数。

类型: int | None 默认值: _Unset

decimal_places

十进制数值允许的小数位数。

类型: int | None 默认值: _Unset

examples

此字段的示例值。

FastAPI 关于声明请求示例数据的文档 中了解更多信息。

类型: list[Any] | None 默认值: None

example

在 OpenAPI 3.1.0 中已弃用(现已使用 JSON Schema 2020-12),尽管仍受支持。请改用 examples。

类型: Any | None 默认值: _Unset

openapi_examples

OpenAPI 特有的示例。

它将被添加到生成的 OpenAPI (例如在 /docs 中可见)。

Swagger UI(提供 /docs 界面)对 OpenAPI 特有示例的支持比对 JSON Schema examples 的支持更好,这是其主要应用场景。

FastAPI 关于声明请求示例数据的文档 中了解更多信息。

类型: dict[str, Example] | None 默认值: None

deprecated

将此参数字段标记为已弃用。

它会影响生成的 OpenAPI(例如在 /docs 中可见)。

类型: deprecated | str | bool | None 默认值: None

include_in_schema

用于决定是否在生成的 OpenAPI 中包含此参数字段。您可能不需要它,但它是可用的。

这会影响生成的 OpenAPI (例如在 /docs 中可见)。

类型: bool 默认值: True

json_schema_extra

任何额外的 JSON schema 数据。

类型: dict[str, Any] | None 默认值: None

**extra

extra 参数已弃用。请改用 json_schema_extra。包含 JSON Schema 使用的额外字段。

类型: Any 默认值: {}

源代码位于 fastapi/param_functions.py
def File(  # noqa: N802
    default: Annotated[
        Any,
        Doc(
            """
            Default value if the parameter field is not set.
            """
        ),
    ] = Undefined,
    *,
    default_factory: Annotated[
        Callable[[], Any] | None,
        Doc(
            """
            A callable to generate the default value.

            This doesn't affect `Path` parameters as the value is always required.
            The parameter is available only for compatibility.
            """
        ),
    ] = _Unset,
    media_type: Annotated[
        str,
        Doc(
            """
            The media type of this parameter field. Changing it would affect the
            generated OpenAPI, but currently it doesn't affect the parsing of the data.
            """
        ),
    ] = "multipart/form-data",
    alias: Annotated[
        str | None,
        Doc(
            """
            An alternative name for the parameter field.

            This will be used to extract the data and for the generated OpenAPI.
            It is particularly useful when you can't use the name you want because it
            is a Python reserved keyword or similar.
            """
        ),
    ] = None,
    alias_priority: Annotated[
        int | None,
        Doc(
            """
            Priority of the alias. This affects whether an alias generator is used.
            """
        ),
    ] = _Unset,
    validation_alias: Annotated[
        str | AliasPath | AliasChoices | None,
        Doc(
            """
            'Whitelist' validation step. The parameter field will be the single one
            allowed by the alias or set of aliases defined.
            """
        ),
    ] = None,
    serialization_alias: Annotated[
        str | None,
        Doc(
            """
            'Blacklist' validation step. The vanilla parameter field will be the
            single one of the alias' or set of aliases' fields and all the other
            fields will be ignored at serialization time.
            """
        ),
    ] = None,
    title: Annotated[
        str | None,
        Doc(
            """
            Human-readable title.
            """
        ),
    ] = None,
    description: Annotated[
        str | None,
        Doc(
            """
            Human-readable description.
            """
        ),
    ] = None,
    gt: Annotated[
        float | None,
        Doc(
            """
            Greater than. If set, value must be greater than this. Only applicable to
            numbers.
            """
        ),
    ] = None,
    ge: Annotated[
        float | None,
        Doc(
            """
            Greater than or equal. If set, value must be greater than or equal to
            this. Only applicable to numbers.
            """
        ),
    ] = None,
    lt: Annotated[
        float | None,
        Doc(
            """
            Less than. If set, value must be less than this. Only applicable to numbers.
            """
        ),
    ] = None,
    le: Annotated[
        float | None,
        Doc(
            """
            Less than or equal. If set, value must be less than or equal to this.
            Only applicable to numbers.
            """
        ),
    ] = None,
    min_length: Annotated[
        int | None,
        Doc(
            """
            Minimum length for strings.
            """
        ),
    ] = None,
    max_length: Annotated[
        int | None,
        Doc(
            """
            Maximum length for strings.
            """
        ),
    ] = None,
    pattern: Annotated[
        str | None,
        Doc(
            """
            RegEx pattern for strings.
            """
        ),
    ] = None,
    regex: Annotated[
        str | None,
        Doc(
            """
            RegEx pattern for strings.
            """
        ),
        deprecated(
            "Deprecated in FastAPI 0.100.0 and Pydantic v2, use `pattern` instead."
        ),
    ] = None,
    discriminator: Annotated[
        str | None,
        Doc(
            """
            Parameter field name for discriminating the type in a tagged union.
            """
        ),
    ] = None,
    strict: Annotated[
        bool | None,
        Doc(
            """
            If `True`, strict validation is applied to the field.
            """
        ),
    ] = _Unset,
    multiple_of: Annotated[
        float | None,
        Doc(
            """
            Value must be a multiple of this. Only applicable to numbers.
            """
        ),
    ] = _Unset,
    allow_inf_nan: Annotated[
        bool | None,
        Doc(
            """
            Allow `inf`, `-inf`, `nan`. Only applicable to numbers.
            """
        ),
    ] = _Unset,
    max_digits: Annotated[
        int | None,
        Doc(
            """
            Maximum number of digits allowed for decimal values.
            """
        ),
    ] = _Unset,
    decimal_places: Annotated[
        int | None,
        Doc(
            """
            Maximum number of decimal places allowed for decimal values.
            """
        ),
    ] = _Unset,
    examples: Annotated[
        list[Any] | None,
        Doc(
            """
            Example values for this field.

            Read more about it in the
            [FastAPI docs for Declare Request Example Data](https://fastapi.org.cn/tutorial/schema-extra-example/)
            """
        ),
    ] = None,
    example: Annotated[
        Any | None,
        deprecated(
            "Deprecated in OpenAPI 3.1.0 that now uses JSON Schema 2020-12, "
            "although still supported. Use examples instead."
        ),
    ] = _Unset,
    openapi_examples: Annotated[
        dict[str, Example] | None,
        Doc(
            """
            OpenAPI-specific examples.

            It will be added to the generated OpenAPI (e.g. visible at `/docs`).

            Swagger UI (that provides the `/docs` interface) has better support for the
            OpenAPI-specific examples than the JSON Schema `examples`, that's the main
            use case for this.

            Read more about it in the
            [FastAPI docs for Declare Request Example Data](https://fastapi.org.cn/tutorial/schema-extra-example/#using-the-openapi_examples-parameter).
            """
        ),
    ] = None,
    deprecated: Annotated[
        deprecated | str | bool | None,
        Doc(
            """
            Mark this parameter field as deprecated.

            It will affect the generated OpenAPI (e.g. visible at `/docs`).
            """
        ),
    ] = None,
    include_in_schema: Annotated[
        bool,
        Doc(
            """
            To include (or not) this parameter field in the generated OpenAPI.
            You probably don't need it, but it's available.

            This affects the generated OpenAPI (e.g. visible at `/docs`).
            """
        ),
    ] = True,
    json_schema_extra: Annotated[
        dict[str, Any] | None,
        Doc(
            """
            Any additional JSON schema data.
            """
        ),
    ] = None,
    **extra: Annotated[
        Any,
        Doc(
            """
            Include extra fields used by the JSON Schema.
            """
        ),
        deprecated(
            """
            The `extra` kwargs is deprecated. Use `json_schema_extra` instead.
            """
        ),
    ],
) -> Any:
    return params.File(
        default=default,
        default_factory=default_factory,
        media_type=media_type,
        alias=alias,
        alias_priority=alias_priority,
        validation_alias=validation_alias,
        serialization_alias=serialization_alias,
        title=title,
        description=description,
        gt=gt,
        ge=ge,
        lt=lt,
        le=le,
        min_length=min_length,
        max_length=max_length,
        pattern=pattern,
        regex=regex,
        discriminator=discriminator,
        strict=strict,
        multiple_of=multiple_of,
        allow_inf_nan=allow_inf_nan,
        max_digits=max_digits,
        decimal_places=decimal_places,
        example=example,
        examples=examples,
        openapi_examples=openapi_examples,
        deprecated=deprecated,
        include_in_schema=include_in_schema,
        json_schema_extra=json_schema_extra,
        **extra,
    )