Skip to content

Responses Reference

This page documents Quater response conversion and explicit response classes.

Prerequisites

Read Public API. Most handlers can return plain Python values; use response classes when you need status, headers, redirects, or streams.

python
from quater import JSONResponse, RedirectResponse, Response, StreamResponse

Automatic Return Values

Handler returnsQuater sends
dict, list, tuple, dataclass, msgspec.StructJSONResponse
strTextResponse
bytes, bytearray, memoryviewBytesResponse
NoneEmptyResponse(status_code=204)
Response instanceSent as-is

If Quater cannot convert the value, it raises ResponseConversionError and returns a 500 Internal Server Error.

Response

Added in 0.1.0a1.

python
Response(
    body: bytes = b"",
    *,
    status_code: int = 200,
    headers: HeaderItems | Mapping[str, str] | None = None,
    content_type: str | None = None,
) -> None
ParameterTypeDefaultDescription
bodybytesb""Raw response body.
status_codeint200HTTP status code.
headersHeaderItems | Mapping[str, str] | NoneNoneResponse headers.
content_typestr | NoneNoneContent type added when no header exists.

JSONResponse

python
JSONResponse(content: object, *, status_code: int = 200, headers: HeaderItems | Mapping[str, str] | None = None)

Serializes content with Quater's msgspec JSON encoder.

TextResponse

python
TextResponse(
    content: str,
    *,
    status_code: int = 200,
    headers: HeaderItems | Mapping[str, str] | None = None,
    content_type: str = "text/plain; charset=utf-8",
)

HTMLResponse

python
HTMLResponse(content: str, *, status_code: int = 200, headers: HeaderItems | Mapping[str, str] | None = None)

Sets content-type: text/html; charset=utf-8.

BytesResponse

python
BytesResponse(
    content: bytes | bytearray | memoryview,
    *,
    status_code: int = 200,
    headers: HeaderItems | Mapping[str, str] | None = None,
    content_type: str = "application/octet-stream",
)

StreamResponse

python
StreamResponse(
    body_iterator: AsyncIterable[bytes],
    *,
    status_code: int = 200,
    headers: HeaderItems | Mapping[str, str] | None = None,
    content_type: str = "application/octet-stream",
)

Use this for async byte streams. Resource finalizers stay attached until the adapter finishes consuming the stream.

RedirectResponse

python
RedirectResponse(
    location: str,
    *,
    status_code: int = 307,
    headers: HeaderItems | Mapping[str, str] | None = None,
)

Default 307 preserves the HTTP method.

EmptyResponse

python
EmptyResponse(*, status_code: int = 204, headers: HeaderItems | Mapping[str, str] | None = None)

Both helpers are available on every response class.

python
response.set_cookie(
    key: str,
    value: str = "",
    *,
    max_age: int | None = None,
    expires: int | None = None,
    path: str | None = "/",
    domain: str | None = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: Literal["lax", "strict", "none"] | None = "lax",
) -> None
ParameterTypeDefaultDescription
keystrCookie name. Must be a valid RFC 6265 token.
valuestr""Cookie value. Must be a valid RFC 6265 cookie-octet string.
max_ageint | NoneNoneMax age in seconds.
expiresint | NoneNoneExpiry as a Unix timestamp.
pathstr | None"/"Cookie path.
domainstr | NoneNoneCookie domain.
secureboolFalseSet the Secure flag.
httponlyboolFalseSet the HttpOnly flag.
samesiteLiteral["lax", "strict", "none"] | None"lax"SameSite policy. "none" requires secure=True.
python
response.delete_cookie(
    key: str,
    *,
    path: str | None = "/",
    domain: str | None = None,
    secure: bool = False,
    httponly: bool = False,
    samesite: Literal["lax", "strict", "none"] | None = "lax",
) -> None
ParameterTypeDefaultDescription
keystrCookie name to delete.
pathstr | None"/"Must match the path used when the cookie was set.
domainstr | NoneNoneMust match the domain used when the cookie was set.
secureboolFalseRequired for __Secure- and __Host- prefixed cookies.
httponlyboolFalseSet the HttpOnly flag.
samesiteLiteral["lax", "strict", "none"] | None"lax"Required when the deletion response is sent cross-site. "none" requires secure=True.

Complete Example

python
from quater import JSONResponse, Quater, RedirectResponse

app = Quater()


@app.post("/orders")
async def create_order() -> JSONResponse:
    return JSONResponse({"id": "ord_1001"}, status_code=201)


@app.get("/orders/latest")
async def latest_order() -> RedirectResponse:
    return RedirectResponse("/orders/ord_1001")

Expected create response:

json
{
  "id": "ord_1001"
}

What Can Go Wrong

Cannot convert 'set' into a response : Return a supported value or create a Response explicitly.

Invalid response header name : Header names must be valid HTTP token names.

Invalid response header value : Header values cannot contain unsafe control characters.

Also See

Released under the MIT License.