Routing API
Router
The main router class.
Constructor
Router(prefix: str = "")
| Parameter | Type | Description |
|---|---|---|
prefix | str | Optional URL prefix |
Methods
add_route
add_route(path: str, endpoint: Callable, *, methods: set[str] | list[str] | tuple[str, ...] = ("GET",), name: str | None = None) -> Route
Add an HTTP route.
add_websocket
add_websocket(path: str, endpoint: Callable, *, name: str | None = None) -> WebSocketRoute
Add a WebSocket route.
route
route(path: str, *, methods: set[str] | list[str] | tuple[str, ...] = ("GET",), name: str | None = None) -> Callable
Decorator for registering an HTTP route.
get
get(path: str, *, name: str | None = None) -> Callable
post
post(path: str, *, name: str | None = None) -> Callable
put
put(path: str, *, name: str | None = None) -> Callable
patch
patch(path: str, *, name: str | None = None) -> Callable
delete
delete(path: str, *, name: str | None = None) -> Callable
head
head(path: str, *, name: str | None = None) -> Callable
options
options(path: str, *, name: str | None = None) -> Callable
websocket
websocket(path: str, *, name: str | None = None) -> Callable
include_router
include_router(other: Router) -> None
match
match(path: str, method: str) -> RouteMatch
Match an HTTP request to a route.
match_websocket
match_websocket(path: str) -> WebSocketMatch
Match a WebSocket connection to a route.
url_for
url_for(name: str, **params: Any) -> str
Generate a URL for a named route.
Properties
routes— All HTTP routeswebsocket_routes— All WebSocket routesroute_count— Number of HTTP routeswebsocket_count— Number of WebSocket routestotal_count— Total routes
Route
HTTP route definition.
Attributes
path— The URL path patternendpoint— The endpoint functionmethods— HTTP methods allowedname— Route nameregex— Compiled regexparameters— Parameter names
match_path
match_path(path: str) -> dict[str, Any] | None
WebSocketRoute
WebSocket route definition.
Attributes
path— WebSocket URL path patternendpoint— The endpoint functionname— Route nameregex— Compiled regexparameters— Parameter names
match_path
match_path(path: str) -> dict[str, Any] | None
Supported Converters
str, int, float, path, uuid, slug