Request
wybthon.request¶
request
¶
The request being server-rendered, and the response head the render declares.
Components call http_status and
http_header to shape the HTTP response while
they render on the server, and
get_request_event to read the request.
In the browser all three are harmless: there's no request, and the
response head was sent long ago.
from wybthon import component, html, http_status
@component
def NotFound():
http_status(404)
return html(t"<h1>Not found</h1>")
On the server, pass a RequestEvent to the
render function and read its response afterward:
from wybthon import RequestEvent
from wybthon.server import render_to_string
event = RequestEvent(url=request.path, request=request)
body = render_to_string(App(), event=event)
return Response(body, status=event.response.status, headers=event.response.header_items())
Declarations belong to the reactive scope that made them, as in Solid 2.0: when that scope is disposed before the response is committed (an error boundary recovering, a branch switching), the previous status or header is restored. Once the head is committed (the render finished, or a stream sent its shell), further declarations are ignored.
Classes:
| Name | Description |
|---|---|
ResponseHead |
The status and headers a server render declares. |
RequestEvent |
One request being rendered on the server. |
Functions:
| Name | Description |
|---|---|
get_request_event |
Return the request being handled on the server, or |
http_status |
Declare the response status while the calling scope is alive (server only). |
http_header |
Declare a response header while the calling scope is alive (server only). |
set_cookie |
Set a cookie on the response being handled (server only). |
ResponseHead
dataclass
¶
ResponseHead(status: int = 200, status_text: str | None = None, headers: dict[str, list[str]] = dict(), committed: bool = False, _statuses: list[tuple[object, int, str | None]] = list(), _headers: list[tuple[object, str, str, bool]] = list(), _base: tuple[int, str | None, dict[str, list[str]]] | None = None)
The status and headers a server render declares.
Attributes:
| Name | Type | Description |
|---|---|---|
status |
int
|
The HTTP status code. |
status_text |
str | None
|
An optional reason phrase. |
headers |
dict[str, list[str]]
|
Header values by lowercase name, in declaration order. |
committed |
bool
|
Set once the head can no longer change (the render finished, or a stream sent its shell). |
Methods:
| Name | Description |
|---|---|
header_items |
Return |
RequestEvent
dataclass
¶
RequestEvent(url: str = '/', request: Any = None, locals: dict[str, Any] = dict(), response: ResponseHead = ResponseHead(), method: str = 'GET', headers: dict[str, str] = dict())
One request being rendered on the server.
Attributes:
| Name | Type | Description |
|---|---|---|
url |
str
|
The request path and query, read by the router. |
request |
Any
|
The framework's request object, passed through as is. |
locals |
dict[str, Any]
|
Per-request values for the application (the signed-in user, a database session, and so on). |
response |
ResponseHead
|
The response head the render declares. |
method |
str
|
The HTTP method. |
headers |
dict[str, str]
|
Request headers by lowercase name. |
get_request_event
¶
get_request_event() -> RequestEvent | None
Return the request being handled on the server, or None in the browser.
That's the request a server render is for, or the one a server function call came in with.
http_status
¶
Declare the response status while the calling scope is alive (server only).
The most recent live declaration wins. When the declaring scope is disposed before the head is committed, its declaration is removed and the status falls back to the latest one still alive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
int
|
The HTTP status code. |
required |
text
|
str | None
|
An optional reason phrase. |
None
|
http_header
¶
Declare a response header while the calling scope is alive (server only).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The header name (case-insensitive). |
required |
value
|
str
|
The header value. |
required |
append
|
bool
|
Add another value instead of replacing existing ones
(for |
False
|
set_cookie
¶
set_cookie(name: str, value: str, *, max_age: int | None = None, path: str = '/', domain: str | None = None, secure: bool = True, http_only: bool = True, same_site: Literal['lax', 'strict', 'none'] = 'lax') -> None
Set a cookie on the response being handled (server only).
Call it from a server function, or while a page renders on the
server. Pass max_age=0 to delete a cookie. In the browser it does
nothing.
What's in this module¶
The request a server render is answering, and the response head it
declares. Components call these helpers while they render on the
server; in the browser they're harmless no-ops. Every name here is also
exported from wybthon, matching Solid 2.0's getRequestEvent,
httpStatus, and httpHeader.
| Name | Description |
|---|---|
RequestEvent |
RequestEvent(url="/", request=None, locals={}, response=ResponseHead()): pass it to a server render and read response afterward. |
ResponseHead |
status, status_text, headers (lowercase name to a list of values), committed, and header_items() for an HTTP response. |
get_request_event |
The RequestEvent being rendered, or None in the browser. |
http_status |
http_status(code, text=None): declare the response status. |
http_header |
http_header(name, value, *, append=False): declare a header; append=True adds another value (for set-cookie). |
Declarations belong to the reactive scope that made them. When that
scope is disposed before the response is committed (an error boundary
recovering, a Show switching branches), the previous status or header
is restored. The head is committed once the render finishes, or when a
stream sends its shell; later declarations are ignored.
from wybthon import RequestEvent, component, get_request_event, html, http_header, http_status
from wybthon.server import render_to_string
@component
def NotFound():
http_status(404)
return html(t"<h1>Not found</h1>")
@component
def Greeting():
event = get_request_event()
user = event.locals.get("user") if event is not None else None
http_header("Vary", "Cookie")
return html(t"<p>Hello, {user or 'guest'}</p>")
event = RequestEvent(url="/missing", locals={"user": "Ada"})
body = render_to_string(NotFound(), event=event)
event.response.status # 404
event.response.header_items() # [] for NotFound; [("vary", "Cookie")] for Greeting
See also¶
- Server:
render_to_stringandrender_to_stream - Router: render
http_status(404)from anot_foundcomponent - Concepts: Server rendering