Skip to main content

Body

Declares a parameter to be extracted from the request's JSON body, allowing for metadata configuration, validation constraints, and OpenAPI documentation customization. It supports embedding parameters as keys within the JSON body and provides extensive options for data validation and schema generation.

def Body(
default: Any = Undefined,
default_factory: Callable[[], Any] | None = _Unset,
embed: bool | None = null,
media_type: str = application/json,
alias: str | None = null,
alias_priority: int | None = _Unset,
validation_alias: str | AliasPath | AliasChoices | None = null,
serialization_alias: str | None = null,
title: str | None = null,
description: str | None = null,
gt: float | None = null,
ge: float | None = null,
lt: float | None = null,
le: float | None = null,
min_length: int | None = null,
max_length: int | None = null,
pattern: str | None = null,
regex: str | None = null,
discriminator: str | None = null,
strict: bool | None = _Unset,
multiple_of: float | None = _Unset,
allow_inf_nan: bool | None = _Unset,
max_digits: int | None = _Unset,
decimal_places: int | None = _Unset,
examples: list[Any] | None = null,
example: Any | None = _Unset,
openapi_examples: dict[str, Example] | None = null,
deprecated: deprecated | str | bool | None = null,
include_in_schema: bool = true,
json_schema_extra: dict[str, Any] | None = null,
extra: Any = null
) - > Any

Declares a parameter that should be extracted from the request body. Use this to configure validation, documentation, and metadata for JSON body fields in FastAPI path operations.

Parameters

NameTypeDescription
defaultAny = UndefinedDefault value if the parameter field is not set.
default_factory`Callable[[], Any]None` = _Unset
embed`boolNone` = null
media_typestr = application/jsonThe media type of this parameter field used for OpenAPI documentation generation.
alias`strNone` = null
alias_priority`intNone` = _Unset
validation_alias`strAliasPath
serialization_alias`strNone` = null
title`strNone` = null
description`strNone` = null
gt`floatNone` = null
ge`floatNone` = null
lt`floatNone` = null
le`floatNone` = null
min_length`intNone` = null
max_length`intNone` = null
pattern`strNone` = null
regex`strNone` = null
discriminator`strNone` = null
strict`boolNone` = _Unset
multiple_of`floatNone` = _Unset
allow_inf_nan`boolNone` = _Unset
max_digits`intNone` = _Unset
decimal_places`intNone` = _Unset
examples`list[Any]None` = null
example`AnyNone` = _Unset
openapi_examples`dict[str, Example]None` = null
deprecated`deprecatedstr
include_in_schemabool = trueWhether to include this parameter in the generated OpenAPI documentation.
json_schema_extra`dict[str, Any]None` = null
extraAny = nullDeprecated keyword arguments for extra JSON Schema fields; use 'json_schema_extra' instead.

Returns

TypeDescription
AnyA FastAPI parameter metadata object used by the dependency injection system to parse and validate the request body.