Inspect client information
An endpoint needs connection metadata such as the caller's host.
FastAPI · Class
Request is FastAPI's raw HTTP request object, injected into path operations or dependencies through a type annotation. Use it to inspect request-level data directly when standard parameter declarations do not provide the needed access.
Request is the raw incoming HTTP request object that FastAPI injects when a path-operation parameter is annotated with Request. It exposes request metadata and underlying connection information without applying FastAPI's normal parameter validation, conversion, or OpenAPI documentation to values read directly from it.
Request(scope, receive=empty_receive, send=empty_send)| Name | Type | Description |
|---|---|---|
scope | Scope | ASGI scope containing request metadata. |
receive | Receive | ASGI receive callable; defaults to empty_receive. |
send | Send | ASGI send callable; defaults to empty_send. |
A Request instance representing the incoming HTTP request.
from fastapi import Request.from fastapi import FastAPI, Request
app = FastAPI()
@app.get("/client")
async def client_info(request: Request):
return {"client_host": request.client.host if request.client else None}Declares Request as a path-operation parameter and returns the client's host.
An endpoint needs connection metadata such as the caller's host.
Application code must access raw request details not modeled as FastAPI parameters.
A dependency needs protocol-neutral access across HTTP and WebSocket connections.
Annotate a path-operation or dependency parameter with Request, and FastAPI supplies the raw request object. It can then be used to inspect request data and metadata directly.
Follow-up: When should HTTPConnection be used instead? Use it when a dependency must work for both HTTP requests and WebSockets.
Request provides direct access to the incoming request, including connection metadata such as the client address. The documented example reads request.client.host and returns it.
Follow-up: What should code do if the client information is unavailable? Treat request.client as optional and handle a missing value rather than dereferencing it unconditionally.
FastAPI exposes Request directly as a convenience, while the object comes from Starlette underneath. This means its behavior follows the underlying Starlette request implementation.
Follow-up: What is a practical consequence of that layering? Consult Starlette's Request documentation for details not covered by FastAPI's reference page.
HTTPConnectionWebSocketAPIRouteFastAPI