RequestIdMiddleware Class

Pure-ASGI middleware that sets x-request-id on every HTTP response.

The resolved request ID is stored in scope["state"] under <xref:REQUEST_ID_STATE_KEY> so that protocol-specific code (e.g. the Responses package) can read it for error body enrichment.

Unlike BaseHTTPMiddleware, this passes the receive callable through to the inner application untouched, preserving request.is_disconnected() behaviour.

Constructor

RequestIdMiddleware(app: Callable[[MutableMapping[str, Any], Callable[[], Awaitable[MutableMapping[str, Any]]], Callable[[MutableMapping[str, Any]], Awaitable[None]]], Awaitable[None]])

Parameters

Name Description
app
Required
<xref:ASGIApp>

The inner ASGI application.