/ justapi / docs_site / src / content / docs / how-to / custom-request-route.md
swadhinbiswas πŸš€ release: 2.0.8 β€” write-path fix (ADR-080), fork-safe GIL pool (ADR-081), HTTP/3 (ADR-082), Security DI fix, docs honesty pass Β· 8/6/2026
731fffe
52 lines Β· 1.5 KB
Raw Download

title: Custom Request Class
description: Extend the Request class in JustAPI for custom request behavior.
keywords: [JustAPI, custom request, extend, Request]

Custom Request Class

JustAPI's routing is fully native (Rust matchit), so there is no
replaceable APIRoute/route_class mechanism β€” routes are registered on
JustAPIApp. You can subclass justapi.Request and accept it as a parameter
to customize how a handler reads the request.

from justapi import JustAPIApp, Request

class CustomRequest(Request):
    async def json(self):
        body = await super().json()
        # pre-process the body before application logic
        return body

app = JustAPIApp()

@app.post("/items")
async def create_item(request: CustomRequest):
    data = await request.json()
    return data

For per-route cross-cutting behavior (timing, auth, header injection), use the
native middleware mechanisms instead:

import time
from justapi import JustAPIApp

app = JustAPIApp()

@app.middleware("http")
async def timing(request, call_next):
    start = time.time()
    response = await call_next(request)
    response.headers["X-Process-Time"] = str(round(time.time() - start, 4))
    return response

See Also