Skip to main content

Body

This class represents a request body, allowing for detailed configuration of its validation and documentation. It supports embedding the body directly, specifying media types, and providing examples for API documentation. It extends FieldInfo to leverage its validation and schema generation capabilities.

Attributes

AttributeTypeDescription
embed`boolNone`
media_typestr = "application/json"The media type of the request body, affecting how the client sends the data and how the server parses it.
example`AnyNone` = _Unset
include_in_schemabool = trueWhether to include this parameter in the generated OpenAPI schema.
openapi_examples`dict[str, Example]None`

Constructor

Signature

def Body(
default: Any = Undefined,
default_factory: Callable[[], Any]| None = _Unset,
annotation: Any | None = null,
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: Annotated[str | None, deprecated("Deprecated in FastAPI 0.100.0 and Pydantic v2, use `pattern` instead.")] = 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: 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: dict[str, [Example](../openapi/models/example.md?sid=fastapi_openapi_models_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
) - > null

Parameters

NameTypeDescription
defaultAny = UndefinedThe default value for the body.
default_factory`Callable[[], Any]None` = _Unset
annotation`AnyNone` = null
embed`boolNone` = null
media_typestr = "application/json"The media type of the request body.
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`Annotated[strNone, deprecated("Deprecated in FastAPI 0.100.0 and Pydantic v2, use pattern instead.")]` = 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`Annotated[AnyNone, deprecated("Deprecated in OpenAPI 3.1.0 that now uses JSON Schema 2020-12, although still supported. Use examples instead.")]` = _Unset
openapi_examples`dict[str, Example]None` = null
deprecated`deprecatedstr
include_in_schemabool = trueWhether to include the field in the OpenAPI schema.
json_schema_extra`dict[str, Any]None` = null
extraAny = nullAdditional keyword arguments.