Skip to content

Responses Reference

view.response

HTMLContent = Union[TextIO, str, Path, DOMNode] module-attribute

T = TypeVar('T') module-attribute

__all__ = ('Response', 'HTML', 'JSON', 'to_response') module-attribute

HTML

Bases: Response[HTMLContent]

HTML response wrapper.

__init__(body: HTMLContent, status: int = 200, headers: dict[str, str] | None = None) -> None

from_file(path: str | Path) -> HTML async classmethod

JSON

Bases: Response[Dict[str, Any]]

JSON response wrapper.

__init__(body: dict[str, Any], status: int = 200, headers: dict[str, str] | None = None) -> None

Response

Bases: Generic[T]

Wrapper for responses.

body = body instance-attribute

headers = headers or {} instance-attribute

status = status instance-attribute

translate = body_translate instance-attribute

__init__(body: T, status: int = 200, headers: dict[str, str] | None = None, *, body_translate: BodyTranslateStrategy | None = _Find) -> None

__view_result__() -> ViewResult

cookie(key: str, value: str = '', *, max_age: int | None = None, expires: int | DateTime | None = None, path: str | None = None, domain: str | None = None, http_only: bool = False, same_site: SameSite = 'lax', partitioned: bool = False, secure: bool = False) -> None

Set a cookie.

Parameters:

Name Type Description Default
key str

Cookie name.

required
value str

Cookie value.

''
max_age int | None

Max age of the cookies.

None
expires int | datetime | None

When the cookie expires.

None
domain str | None

Domain the cookie is valid at.

None
http_only bool

Whether the cookie should be HTTP only.

False
same_site SameSite

SameSite setting for the cookie.

'lax'
partitioned bool

Whether to tie it to the top level site.

False
secure bool

Whether the cookie should enforce HTTPS.

False

to_response(result: ViewResult) -> Response[ResponseBody]

Cast a result from a route function to a Response object.