Skip to content

Basic Request Properties

python
@app.get("/example")
async def example_handler(req: Request, res):
    # Basic request information
    method = req.method        # HTTP method (GET, POST, etc.)
    url = req.url              # Full URL object
    path = req.path            # Request path (/example)
    headers = req.headers      # Headers dictionary
    client_ip = req.client     # Client address (IP, port)

Query Parameters

Access URL query parameters (after the ? in the URL):

python
@app.get("/search")
async def search_handler(req: Request, res):
    # For URL: /search?q=nexios&page=2
    query = req.query_params.get("q")       # "nexios"
    page = req.query_params.get("page")     # "2"
    all_params = dict(req.query_params)    # {'q': 'nexios', 'page': '2'}

Path Parameters

Access named parameters from the route path:

python
@app.get("/users/{user_id}")
async def user_handler(req: Request, res):
    # For URL: /users/123
    user_id = req.path_params["user_id"]  # "123"
    # Or directly as function parameter (shown above)

Request Body

JSON Data

python
@app.post("/data")
async def data_handler(req: Request, res):
    json_data = await req.json  # Parses JSON body

Form Data

python
@app.post("/submit")
async def submit_handler(req: Request, res):
    form_data = await req.form  # Parses both URL-encoded and multipart forms
    username = form_data.get("username")

File Uploads

python
@app.post("/upload")
async def upload_handler(req: Request, res):
    files = await req.files      # Dictionary of uploaded files
    file = files.get("document") # Access specific file
    if file:
        filename = file.filename
        content = await file.read()

Raw Body

python
@app.post("/raw")
async def raw_handler(req: Request, res):
    body_bytes = await req.body  # Raw bytes
    body_text = await req.text  # Decoded text

Cookies

python
@app.get("/profile")
async def profile_handler(req: Request, res):
    session_id = req.cookies.get("session_id")

Client Information

python
@app.get("/client-info")
async def client_info_handler(req: Request, res):
    user_agent = req.user_agent
    client_ip = req.client.host if req.client else None
    origin = req.origin

State and Middleware Data

python
@app.get("/auth")
async def auth_handler(req: Request, res):
    # Access data added by middleware
    user = req.user
    session = req.session  # Requires session middleware
    custom_data = req.state.get("custom_data")

URL Construction

python
@app.get("/links")
async def links_handler(req: Request, res):
    absolute_url = req.build_absolute_uri("/api/resource")
    # Returns full URL like "https://example.com/api/resource"

Request Type Detection

Nexios provides convenient properties to quickly check the type and characteristics of incoming requests:

Content Type Flags

python
@app.post("/api/endpoint")
async def handle_request(req: Request, res):
    # Check content type
    if req.is_json:
        data = await req.json
        # Handle JSON data
    elif req.is_form:
        data = await req.form
        # Handle form data
    elif req.is_multipart:
        files = await req.files
        # Handle file uploads
    elif req.is_urlencoded:
        data = await req.form
        # Handle URL-encoded form data

Request State Flags

python
@app.post("/process")
async def process_request(req: Request, res):
    # Check if request has various components
    if req.has_cookie:
        session_id = req.cookies.get("session")
    
    if req.has_files:
        files = await req.files
        # Process uploaded files
    
    if req.has_body:
        # Request contains body data
        if req.content_length > 1000000:  # 1MB
            return res.status(413).text("File too large")
    
    if req.is_authenticated:
        user_id = req.user.id
        # Handle authenticated request
    
    if req.has_session:
        session_data = req.session
        # Access session data

Request Type Properties

PropertyDescriptionExample
req.is_jsonTrue if Content-Type is application/jsonJSON API requests
req.is_formTrue if Content-Type is form data (URL-encoded or multipart)HTML forms
req.is_multipartTrue if Content-Type is multipart/form-dataFile uploads
req.is_urlencodedTrue if Content-Type is application/x-www-form-urlencodedSimple forms
req.has_cookieTrue if request contains cookiesSession management
req.has_filesTrue if request contains uploaded filesFile upload detection
req.has_bodyTrue if request has a bodyPOST/PUT/PATCH requests
req.is_authenticatedTrue if user is authenticatedAuthenticated requests
req.has_sessionTrue if session middleware is availableSession-enabled requests

Existing Request Flags

Nexios also provides additional request detection properties:

python
@app.get("/responsive")
async def responsive_handler(req: Request, res):
    # Check request characteristics
    if req.is_ajax:
        return res.json({"message": "AJAX request"})
    
    if req.is_secure:
        return res.json({"protocol": "HTTPS"})
    
    if req.accepts_json:
        return res.json({"format": "JSON preferred"})
    
    if req.accepts_html:
        return res.html("<h1>HTML Response</h1>")

Header Utilities

python
@app.get("/headers")
async def header_handler(req: Request, res):
    # Check for specific headers
    if req.has_header("authorization"):
        token = req.get_header("authorization")
    
    # Get header with default value
    api_version = req.get_header("x-api-version", "v1")
    
    # Check if header exists
    if req.has_header("x-custom-header"):
        custom_value = req.get_header("x-custom-header")
Method/PropertyDescriptionExample
req.has_header(name)Check if header exists (case-insensitive)req.has_header("content-type")
req.get_header(name, default)Get header value with defaultreq.get_header("x-api-key", "none")
req.is_ajaxTrue if X-Requested-With is XMLHttpRequestAJAX requests
req.is_secureTrue if request uses HTTPSSecure connections
req.accepts_jsonTrue if client accepts JSONAPI responses
req.accepts_htmlTrue if client accepts HTMLWeb page responses

Advanced Features

Streaming Requests

For handling large uploads:

python
@app.post("/stream")
async def stream_handler(req: Request, res):
    async for chunk in req.stream():
        # Process each chunk of the request body
        process_chunk(chunk)

Server Push

python
@app.get("/push")
async def push_handler(req: Request, res):
    await req.send_push_promise("/static/style.css")

The Nexios Request object provides a rich interface for working with incoming HTTP requests, with support for all common web standards and convenient access to request data.