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
66 lines Β· 1.9 KB
Raw Download

title: Response Status Code
description: Set custom HTTP response status codes in JustAPI handlers.
keywords: [JustAPI, status code, HTTP status, response, error handling]

Default Status Code

JustAPI returns 200 OK by default for successful responses that don't use a
Response object.

Explicit Status Code

The status_code= route decorator kwarg (e.g. @app.post("/items", status_code=201))
is OpenAPI documentation metadata only β€” it documents the expected success
code in the generated schema but does not alter the actual HTTP status of a
plain dict/JSON return (which is still 200).

To actually set the response status, return a Response object:

from justapi import JustAPIApp, JSONResponse

app = JustAPIApp()

@app.post("/items")
def create_item():
    return JSONResponse(content={"message": "created"}, status_code=201)

Common Status Codes

Code Meaning Use When
200 OK GET, successful PUT/PATCH
201 Created Successful POST
204 No Content Successful DELETE
301 Moved Permanently Redirect
400 Bad Request Client error
401 Unauthorized Authentication required
403 Forbidden Authenticated but not authorized
404 Not Found Resource doesn't exist
422 Unprocessable Entity Validation error

Returning a Response Directly

For full control, return a Response object:

from justapi import JSONResponse

@app.get("/custom")
def custom_response():
    return JSONResponse(
        content={"message": "custom"},
        status_code=202,
        headers={"X-Custom": "header"},
    )

See Also