Skip to main content
The 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 a Request 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

async json()

Parse and return the request body as JSON.
Returns: Any - The parsed JSON data

async body()

Get the raw request body as bytes.
Returns: bytes - The raw request body

async form()

Parse and return the request body as form data.
Returns: FormData - The parsed form data

is_disconnected()

Check if the client has disconnected.
Returns: bool - True if the client has disconnected

stream()

Stream the request body in chunks.
Returns: AsyncIterator[bytes] - An async iterator of body chunks

url_for(name, **path_params)

Generate a URL for a named route.
str
required
Name of the route.
Any
Path parameters for the route.
Returns: URL - The generated URL

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 the Request 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