Request object provides access to all incoming HTTP request data including headers, body, query parameters, path parameters, cookies, and more. FastAPI uses Starlette’s Request class.
Importing
Usage in Path Operations
You can declare aRequest parameter in your path operation function to access the raw request object:
Attributes
str
The HTTP method (e.g., “GET”, “POST”, “PUT”, “DELETE”).
URL
The full URL of the request, including scheme, host, port, and path.
Headers
HTTP headers from the request. Case-insensitive dict-like object.
QueryParams
Query parameters from the URL. Dict-like object.
dict[str, Any]
Path parameters extracted from the URL path.
dict[str, str]
Cookies from the request.
Address | None
Client address information (host and port).
FastAPI
The FastAPI application instance.
State
State object that can be used to store arbitrary data during the request lifecycle.
dict[str, Any]
ASGI scope dictionary containing all request information.
Methods
json()
json()
body()
body()
form()
form()
async form()
Parse and return the request body as form data.FormData - The parsed form datais_disconnected()
is_disconnected()
is_disconnected()
Check if the client has disconnected.bool - True if the client has disconnectedstream()
stream()
stream()
Stream the request body in chunks.AsyncIterator[bytes] - An async iterator of body chunksurl_for()
url_for()
Common Use Cases
Accessing Headers
Reading Custom Headers
Accessing Client Information
Working with Request State
Reading Raw Request Body
Combining Request with Other Parameters
Note on Direct Usage
While theRequest object provides direct access to all request data, FastAPI’s parameter declaration system (using Query, Path, Body, Header, etc.) is often more convenient and provides automatic validation and documentation. Use the Request object when you need:
- Access to raw request data
- Custom header processing
- Client information
- Request state
- Functionality not covered by FastAPI’s parameter system