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
| Attribute | Type | Description |
|---|---|---|
| embed | `bool | None` |
| media_type | str = "application/json" | The media type of the request body, affecting how the client sends the data and how the server parses it. |
| example | `Any | None` = _Unset |
| include_in_schema | bool = true | Whether 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
| Name | Type | Description |
|---|---|---|
| default | Any = Undefined | The default value for the body. |
| default_factory | `Callable[[], Any] | None` = _Unset |
| annotation | `Any | None` = null |
| embed | `bool | None` = null |
| media_type | str = "application/json" | The media type of the request body. |
| alias | `str | None` = null |
| alias_priority | `int | None` = _Unset |
| validation_alias | `str | AliasPath |
| 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] | None` = null |
| deprecated | `deprecated | str |
| include_in_schema | bool = true | Whether to include the field in the OpenAPI schema. |
| json_schema_extra | `dict[str, Any] | None` = null |
| extra | Any = null | Additional keyword arguments. |