Body
This section will demonstrate how to extract and validate the body using Rapidy.
Description
The HTTP request body is the part of the request that transmits data from the client to the server. It plays a key role in the POST, PUT, and PATCH methods, which are used to create, update, and modify resources.
For example, in a POST request to create a user account, the user data is sent in the request body.
from pydantic import BaseModel
from rapidy.http import post, Body
class UserData(BaseModel):
username: str
password: str
@post('/')
async def handler(
user_data: UserData = Body(),
) -> ...:
Body Attributes
content_type
Defines the expected data type in the body that the server accepts.
More details about enum ContentType can be found here.
Rapidy uses the specified content_type to extract data correctly.
Supported content types:
application/jsonapplication/x-www-form-urlencodedmultipart/form-datatext/*— any MIME type for textual dataapplication/octet-stream
If the server expects a format that Rapidy does not explicitly support (e.g., video/mpeg), the data will be extracted as bytes and passed to the pydantic model without processing.
check_content_type
Determines whether the Content-Type header should be validated.
- When
True(default value),Rapidywill compare the receivedContent-Typeheader with the expectedcontent_type. If they do not match, an error will be returned to the client:
{
"errors": [
{
"type": "ExtractError",
"loc": [
"body"
],
"msg": "Failed to extract body data: Expected Content-Type `text/plain`, got `<current_request_content_type>`"
}
]
}
json_decoder
Allows specifying a custom json_decoder for processing JSON data in the request body.
Works only when content_type="application/json".
By default, Rapidy uses json.loads without parameters.
Equivalent examples:
orTo customize JSON decoding, pass any callable object that accepts a str to json_decoder.
Expected data type: Callable[[str], Any].
Example with a custom decoder:
To use json.loads with parameters or a decoder with arguments, use functools.partial:
import json
from functools import partial
from typing import Any, OrderedDict
from rapidy.http import post, Body
decoder = partial(json.loads, object_pairs_hook=OrderedDict)
@post('/')
async def handler(
data: Any = Body(json_decoder=decoder),
) -> ...:
Extraction Without Validation
Most Body types support data extraction without validation.
Disabling validation is not recommended.
If validation is disabled, the parameter will contain the base aiohttp structure:
More details can be found in the Extraction Without Validation section for each body type.
Ways to Disable Validation:
Explicit Disabling
from pydantic import BaseModel
from rapidy.http import post, Body
class BodyData(BaseModel):
...
@post('/')
async def handler(
data: BodyData = Body(validate=False),
) -> ...:
Using Any
No Type Annotation
If no type is specified, Any will be used by default.
Default Values
Most Body types support default values.
If an HTTP request does not contain a body, the parameter will receive the specified default value (if set).
Usage Examples
Default Value Specified
from pydantic import BaseModel
from rapidy.http import post, Body
class BodyData(BaseModel):
...
@post('/')
async def handler(
data: BodyData = Body('some_data'),
# or
data: BodyData = Body(default_factory=lambda: 'some_data'),
) -> ...:
Optional Request Body
from pydantic import BaseModel
from rapidy.http import post, Body
class BodyData(BaseModel):
...
@post('/')
async def handler(
data: BodyData | None = Body(),
# or
data: Optional[BodyData] = Body(),
# or
data: Union[BodyData, None] = Body(),
) -> ...:
More details can be found in the Default Values section for each body type.