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, h1, http_status
@component
def NotFound():
http_status(404)
return h1("Not found")
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 server-rendered, 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). |
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())
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. |
get_request_event
¶
get_request_event() -> RequestEvent | None
Return the request being server-rendered, or None in the browser.
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
|
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, h1, http_header, http_status, p
from wybthon.server import render_to_string
@component
def NotFound():
http_status(404)
return h1("Not found")
@component
def Greeting():
event = get_request_event()
user = event.locals.get("user") if event is not None else None
http_header("Vary", "Cookie")
return p("Hello, ", user or "guest")
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