Ushka

Requests and responses

What a handler receives and what it can return. Routes themselves are in Routing.

What a handler can receive

Handlers declare what they need and Ushka passes it in:

The request

Attribute What it is
request.method, request.path "GET", "/users/42".
request.path_params The captured path parameters, as a dict.
request.query Query string as a dict. A repeated key (?tag=a&tag=b) gives a list.
request.headers Headers as a dict with lower-case names: request.headers["user-agent"].
request.cookies Cookies. Also used to set them: see Sessions and cookies.
request.session Signed cookie session (dict). Needs a secret key.
await request.body() Raw body (bytes).
await request.text() Body decoded as UTF-8.
await request.json() Parsed JSON.
await request.form() URL-encoded or multipart form fields.
await request.files() Uploaded files, see Uploads.
request.client_ip The visitor's address; honours X-Forwarded-For only from trusted proxies. Also request.scheme, request.is_secure, request.host.
request.stream() Async iterator over body chunks, for large uploads.
request.app The Ushka app (for request.app.config).

Invalid JSON, invalid UTF-8 or a malformed form answers 400 Bad Request; a body larger than the [limits] answers 413 Payload Too Large. You do not need to catch those yourself.

Everything read from the request is Untrusted. Plain text and JSON can carry it freely. The guarded sinks refuse it.

Return values

Return whatever is most natural; Ushka turns it into a response.

You return Response
dict, list, bool, float JSON (application/json); datetime, date, time become ISO 8601 text, Decimal and UUID strings.
str text/plain, or text/html when it contains <html or <!DOCTYPE html>.
bytes The bytes as they are.
int The number as text.
None 204 No Content.
A t-string (Python 3.14+) HTML with every value escaped.
(body, status) or (body, status, headers) That body with that status code (and headers).
Response(...) Used as is.

Anything else raises a TypeError explaining what is allowed. Text and JSON get ; charset=utf-8 in their content-type, and a body of known size is sent with Content-Length.

# routes/items/index.py
async def post(request):
    data = await request.json()
    if not data.get("name"):
        return {"error": "name is required"}, 400
    return {"created": data["name"]}, 201, {"Location": "/items/1"}

For typed bodies that answer 422 on their own, see Validation.

HTML built from request data is refused

A string served as HTML that contains an Untrusted value raises USHKA-G001. Render a template, return a t-string on Python 3.14+, or escape the value with safe.html(). As text/plain or JSON the same value is fine.

Building a Response yourself

from ushka import Response

async def get():
    return Response(
        "id,name\n1,Ada\n",
        status_code=200,
        headers={"Content-Disposition": "attachment; filename=users.csv"},
        media_type="text/csv",
    )

Response(body, status_code=200, headers=None, media_type=None). A dict or list body becomes JSON. Whatever you set on response.headers is sent, and cookies and the session are saved on it automatically.

Redirects

from ushka import redirect, safe

async def post(request):
    form = await request.form()
    # ... check the credentials ...
    return redirect(safe.url(form.get("next"), allow=["/"], default="/"))

redirect(to, status_code=302) also accepts 301, 303, 307 and 308. The target is a guarded sink: a value from the request is refused with USHKA-G003 until it has been checked.

Request data inside a URL. When only a piece of the URL comes from the request (a path parameter, a query value), give the URL as a template of your own and pass the pieces. They are percent-encoded, so //evil.example or ../../admin stay inside their segment, and no safe.url() is needed:

async def get(request, project_id):                       # routes/projects/[project_id]/open.py
    return redirect("/projects/{id}/", id=project_id, query={"tab": request.query.get("tab", "")})

raise HTTPRedirect("/projects/{id}/", id=project_id) takes the same arguments. Use safe.url() instead when the whole target is client data (a next field). A plain f-string, redirect(f"/projects/{project_id}/"), is refused: the mark of project_id survives it, and the guard cannot tell a harmless piece from a whole URL.

Errors

Raise HttpNotFound, HTTPForbidden and friends to answer with an error page, or return (body, status) for a JSON API. See Error handling.

Streaming, SSE and files

Edit this page