Body
This class defines metadata for request body parameters, allowing for detailed configuration of validation rules and OpenAPI schema generation. It supports features like embedding single parameters into a JSON object, specifying media types, and providing multiple examples for documentation. As an extension of field information, it integrates with validation constraints such as length limits, numeric ranges, and pattern matching.
Attributes
| Attribute | Type | Description |
|---|---|---|
| embed | `bool | None` |
| media_type | string = "application/json" | The media type used to describe the content of the request body, defaulting to application/json. |
| example | `Any | None` = _Unset |
| include_in_schema | bool = true | Flag indicating whether this body parameter should be included 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 = None,
embed: bool | None = None,
media_type: str = application/json,
alias: 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: str | None = None,
discriminator: str | None = None,
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 = None,
example: Any | None = _Unset,
openapi_examples: dict[str, [Example](../openapi/models/example.md?sid=fastapi_openapi_models_example)]| None = None,
deprecated: deprecated | str | bool | None = None,
include_in_schema: bool = True,
json_schema_extra: dict[str, Any]| None = None,
extra: Any
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| default | Any = Undefined | The default value for the body parameter. |
| default_factory | `Callable[[], Any] | None` = _Unset |
| annotation | `Any | None` = None |
| embed | `bool | None` = None |
| media_type | str = application/json | The media type of the request body. |
| alias | `str | None` = None |
| alias_priority | `int | None` = _Unset |
| validation_alias | `str | AliasPath |
| 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 | `str | None` = None |
| discriminator | `str | None` = None |
| 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` = None |
| example | `Any | None` = _Unset |
| openapi_examples | `dict[str, Example] | None` = None |
| deprecated | `deprecated | str |
| include_in_schema | bool = True | Whether to include this parameter in the generated OpenAPI schema. |
| json_schema_extra | `dict[str, Any] | None` = None |
| extra | Any | Additional keyword arguments. |
Signature
def Body(
default: Any = Undefined,
default_factory: Callable[[], Any]| None = _Unset,
annotation: Any | None = None,
embed: bool | None = None,
media_type: str = application/json,
alias: str | None = None,
title: str | None = None,
description: str | None = None,
pattern: str | None = None,
examples: list[Any]| None = None,
include_in_schema: bool = True
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| default | Any = Undefined | The default value for the body parameter if none is provided in the request. |
| default_factory | `Callable[[], Any] | None` = _Unset |
| annotation | `Any | None` = None |
| embed | `bool | None` = None |
| media_type | str = application/json | The HTTP media type used to describe the content format in the OpenAPI schema. |
| alias | `str | None` = None |
| title | `str | None` = None |
| description | `str | None` = None |
| pattern | `str | None` = None |
| examples | `list[Any] | None` = None |
| include_in_schema | bool = True | Whether this parameter should be visible in the generated OpenAPI/Swagger documentation. |