Request Information in Nexios π¨ β
Nexios provides a comprehensive Request
object that gives you access to all the information about the incoming HTTP request. This object is automatically passed to your route handlers and contains methods and properties to access request data.
π 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 Validation β
python
@app.post("/validate")
async def validate_handler(req: Request, res):
if not req.valid():
return res.status(400).text("Invalid request")
π 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. π―