Text
Reading the request body as a string.
Description
Text (MIME-type: text/*) is a data type representing a string.
Rapidy works with any text, regardless of its subtype.
Examples: text/plain, text/html, text/css, text/xml, ..., text/*.
Character encoding detection
The charset parameter of the Content-Type header is used for text decoding.
If charset is not specified by the client, the text will be decoded using utf-8.
from rapidy.http import post, Body, ContentType
@post('/')
async def handler(
text_data: str = Body(content_type=ContentType.text_plain),
# or
text_data: str = Body(content_type=ContentType.text_html),
# or any mime-type with type `text`
text_data: str = Body(content_type=ContentType.text_any),
) -> ...:
Sending with curl
Extraction without validation
Disabling validation is not recommended.
If validation is disabled, the parameter will contain the basic aiohttp structure:
Body(content_type=ContentType.text_plain)→str
Ways to disable validation
Explicit disabling
from enum import Enum
from rapidy.http import post, Body, ContentType
class DataEnum(Enum):
test = 'test'
@post('/')
async def handler(
data: DataEnum = Body(validate=False, content_type=ContentType.text_plain),
) -> ...:
Using Any
No Type Annotation
If no type is specified, Any will be set by default.
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 enum import Enum
from rapidy.http import post, Body, ContentType
class DataEnum(Enum):
test = 'test'
@post('/')
async def handler(
data: DataEnum = Body('some_data', content_type=ContentType.text_plain),
# or
data: DataEnum = Body(default_factory=lambda: 'some_data', content_type=ContentType.text_plain),
) -> ...:
Optional Request Body
from enum import Enum
from rapidy.http import post, Body, ContentType
class DataEnum(Enum):
test = 'test'
@post('/')
async def handler(
data: DataEnum | None = Body(content_type=ContentType.text_plain),
# or
data: Optional[DataEnum] = Body(content_type=ContentType.text_plain),
# or
data: Union[DataEnum, None] = Body(content_type=ContentType.text_plain),
) -> ...:
Extracting Raw Data
Rapidy uses the text method of the Request object to retrieve data and passes it to Pydantic for validation.
How data extraction works in Rapidy
Rapidy uses built-in aiohttp mechanisms for data extraction.
More details about the aiohttp.Request object and methods for extracting data from it can be found
here.
If a parameter is annotated as bytes or StreamReader, data is extracted differently.
More details about the StreamReader object can be found here.
bytes
@post('/')
async def handler(
user_data: bytes = Body(content_type=ContentType.text_plain),
# also you can use pydantic validation
user_data: bytes = Body(content_type=ContentType.text_plain, min_length=1),
) -> ...:
Rapidy internal code
StreamReader
from rapidy import StreamReader
@post('/')
async def handler(
user_data: StreamReader = Body(content_type=ContentType.text_plain),
) -> ...:
Rapidy Internal Code
Validation with Pydantic is not supported for StreamReader.
A default value cannot be set for StreamReader.
If you try to set a default value for Body annotated with StreamReader using default or default_factory, an error ParameterCannotUseDefaultError will be raised.