Routing

Routing is defined by the decorators in the handler functions.

from asgikit import Request
from zayt.web import get


@get("hello")
async def handler(request: Request):
    await request.respond_text("Hello, World!")

Path parameters

Parameters can be defined in the handler’s path using the syntax {parameter_name}.

from asgikit import Request
from zayt.web import get


@get("hello/{name}")
async def handler(request: Request):
    name = request.path_params["name"]
    await request.respond_text(f"Hello, {name}!")

Path matching

The default behavior is for a path parameter to match a single path segment. If you want to match the whole path, or a subpath of the request path, use the syntax {*parameter_name}.

from asgikit import Request
from zayt.web import get


@get("hello/{*path}")
async def handler(request: Request):
    path = request.path_params["path"]
    name = " ".join(path.split("/"))
    await request.respond_text(f"Hello, {name}!")

For a request like GET hello/Python/World, the handler will output Hello, Python World!.

You can mix both types of parameters with no problem:

  • {*path}

  • {*path}/literal_segment

  • {normal_param}/{*path}

  • {normal_param}/{*path}/{other_param}