The Request Object

In any component handler, you can access detailed information about the current incoming HTTP request through the request object.

Accessing the Request Object

The request object is automatically passed as the first argument to your component handlers, giving you access to query string parameters, dynamic path slugs, form data, headers, and more.

PYTHON
# The request object is the first parameter
def load_template_context(request, session, db, **props):
    # Access query parameters: /some-page?name=John
    name = request.args.get("name", "Guest")

    # Access dynamic slugs from the URL, e.g., /users/[user_id]
    user_id = request.view_args.get("user_id")

    return {"greeting": f"Hello, {name}!", "user_id": user_id}

def action_login(request, session, db, **props):
    # Access form data from a POST request
    email = request.form.get("email")
    password = request.form.get("password")

    # ... handle login logic
    return {"message": "Login attempt processed."}

Accessing Dynamic URL Parameters

For dynamic routes (using bracket notation like /users/[id]), the URL parameters are available through request.view_args:

PYTHON
def load_template_context(request, session, db, **props):
    # For route /users/[user_id]
    user_id = request.view_args.get("user_id")
    
    # For nested dynamic routes like /blog/[year]/[slug]
    year = request.view_args.get("year")
    slug = request.view_args.get("slug")
    
    return {"user_id": user_id, "year": year, "slug": slug}

The view_args dictionary contains all dynamic segments from the URL path, with the bracketed names as keys.

Flask-Compatible API

The request object in Noventa is modeled after the Flask Request API. This means you can use many of the same methods and attributes you might already be familiar with from Flask development.

For a complete list of available attributes and methods, you can refer to the official Flask Request documentation.

While the request object's API is compatible with Flask's, it's important to know that Noventa is not built on top of Flask. You should not try to import Flask into your project unless it's for type hinting or other non-runtime purposes.