/ justapi / docs_site / src / content / docs / advanced / using-request-directly.md
swadhinbiswas docs: create 23 new advanced guide and how-to recipe pages · 7/25/2026
54132cc
64 lines · 1.5 KB
Raw Download

title: Using the Request Directly
description: Access the raw Starlette Request object for headers, body, and client info in JustAPI.
keywords: [JustAPI, request object, raw request, headers, body, client]

Accessing the Request

Any handler can receive the raw Request object as a parameter:

from starlette.requests import Request
from justapi import JustAPIApp

app = JustAPIApp()

@app.get("/info")
def get_info(request: Request):
    return {
        "method": request.method,
        "url": str(request.url),
        "client": request.client.host if request.client else None,
    }

Reading Headers

@app.get("/headers")
def get_headers(request: Request):
    return {
        "user_agent": request.headers.get("user-agent"),
        "accept": request.headers.get("accept"),
        "authorization": request.headers.get("authorization", "none"),
    }

Reading Body

@app.post("/raw-body")
async def raw_body(request: Request):
    body = await request.body()
    return {"size": len(body), "content": body.decode()}

Path and Query Info

@app.get("/request-info")
def request_info(request: Request):
    return {
        "path": request.url.path,
        "query": str(request.query_params),
        "path_params": dict(request.path_params),
    }

See Also