Skip to content

Requests

What a handler can ask for: the Request (an HTTP request), the WebSocket, and what both share, Connection. See Requests and responses.

Connection

Connection(scope: dict, receive: Callable[[], Awaitable[dict]], path_params: Mapping[str, Any], app: App | None = None)

What an HTTP request and a WebSocket share: path, headers, cookies, query, client, scheme.

Guide: https://3d3n-pyc.github.io/korvex/requests-and-responses/

method instance-attribute

method: str = scope.get('method', 'GET')

The HTTP method; GET for a WebSocket's handshake.

path instance-attribute

path: str = scope['path']

The path the app routes on, without root_path.

client instance-attribute

client: tuple[str, int] | None = tuple(client) if client else None

(host, port) of the client, behind a trusted proxy too; None if unknown.

scheme instance-attribute

scheme: str = scope.get('scheme', 'http')

"https" or "http" ("wss" or "ws" for a WebSocket), as the client used it.

app instance-attribute

app = app

The App handling it.

root_path property

root_path: str

The prefix the app is served under (ASGI root_path); "" if none.

query property

query: MultiDict[str]

The query string's fields, parsed on first use.

cookies property

cookies: dict[str, str]

The cookies sent by the client, parsed on first use.

url_for

url_for(name: str, /, **params: Any) -> str

The full URL of the route named name, with the request's scheme and host.

The Host header comes from the client: for an emailed link, use a configured base URL. Guide: https://3d3n-pyc.github.io/korvex/routes/#links-to-a-route

Request

Request(scope: dict, receive: Callable[[], Awaitable[dict]], path_params: Mapping[str, Any], app: App | None = None)

An incoming HTTP request: a Connection, and its body.

Guide: https://3d3n-pyc.github.io/korvex/requests-and-responses/

method instance-attribute

method: str = scope.get('method', 'GET')

The HTTP method; GET for a WebSocket's handshake.

path instance-attribute

path: str = scope['path']

The path the app routes on, without root_path.

client instance-attribute

client: tuple[str, int] | None = tuple(client) if client else None

(host, port) of the client, behind a trusted proxy too; None if unknown.

scheme instance-attribute

scheme: str = scope.get('scheme', 'http')

"https" or "http" ("wss" or "ws" for a WebSocket), as the client used it.

app instance-attribute

app = app

The App handling it.

root_path property

root_path: str

The prefix the app is served under (ASGI root_path); "" if none.

query property

query: MultiDict[str]

The query string's fields, parsed on first use.

cookies property

cookies: dict[str, str]

The cookies sent by the client, parsed on first use.

url_for

url_for(name: str, /, **params: Any) -> str

The full URL of the route named name, with the request's scheme and host.

The Host header comes from the client: for an emailed link, use a configured base URL. Guide: https://3d3n-pyc.github.io/korvex/routes/#links-to-a-route

body async

body() -> bytes

Reads the whole body, once; later calls return it again.

json async

json() -> JSONValue

Reads the body as JSON; invalid JSON answers a 400.

form async

form() -> FormData

Reads the body as a urlencoded or multipart form; files are UploadedFiles.

Any other content type answers a 415, undecodable text a 400.

BackgroundTasks

BackgroundTasks()

Work to do after the response is sent, in the order it was added.

Async functions are awaited; plain functions run in a thread. Errors are logged, and the client never sees them.

Guide: https://3d3n-pyc.github.io/korvex/requests-and-responses/#background-tasks

add

add(func: Callable[..., Any], /, *args: Any, **kwargs: Any) -> None

Queues func(*args, **kwargs) to run after the response.

WebSocket

WebSocket(scope: dict, receive: Callable, send: Callable, path_params: Mapping[str, Any], app: App | None = None)

A WebSocket connection, passed to @app.websocket handlers.

Guide: https://3d3n-pyc.github.io/korvex/websockets/

method instance-attribute

method: str = scope.get('method', 'GET')

The HTTP method; GET for a WebSocket's handshake.

path instance-attribute

path: str = scope['path']

The path the app routes on, without root_path.

client instance-attribute

client: tuple[str, int] | None = tuple(client) if client else None

(host, port) of the client, behind a trusted proxy too; None if unknown.

scheme instance-attribute

scheme: str = scope.get('scheme', 'http')

"https" or "http" ("wss" or "ws" for a WebSocket), as the client used it.

app instance-attribute

app = app

The App handling it.

root_path property

root_path: str

The prefix the app is served under (ASGI root_path); "" if none.

query property

query: MultiDict[str]

The query string's fields, parsed on first use.

cookies property

cookies: dict[str, str]

The cookies sent by the client, parsed on first use.

subprotocols instance-attribute

subprotocols: list[str] = list(scope.get('subprotocols', []))

Subprotocols the client asked for, to pick one in accept().

url_for

url_for(name: str, /, **params: Any) -> str

The full URL of the route named name, with the request's scheme and host.

The Host header comes from the client: for an emailed link, use a configured base URL. Guide: https://3d3n-pyc.github.io/korvex/routes/#links-to-a-route

accept async

accept(subprotocol: str | None = None, *, headers: HeaderItems = ()) -> None

Accepts the connection, optionally with one of subprotocols.

receive async

receive() -> str | bytes

The next message, text or binary; raises WebSocketDisconnect once the client has gone.

close async

close(code: int = 1000, reason: str = '') -> None

Closes the connection. Before accept(), it refuses it (403).

WebSocketDisconnect

WebSocketDisconnect(code: int = 1000)

Raised by WebSocket.receive() once the client has disconnected.

UploadedFile

UploadedFile(filename: str, content_type: str, data: bytes)

A file sent in a multipart/form-data form.

request.query and await request.form() are a MultiDict:

MultiDict

MultiDict(lists: dict[str, list[_V]] | None = None)

Names with one or more values, in order: [name] and get() give the first, get_all() all.

get_all

get_all(name: str) -> list[_V]

Every value of name, in order; [] if there's none.

multi_items

multi_items() -> list[tuple[str, _V]]

Every (name, value), a name repeated for each of its values.