Skip to content

Response Cookies and Headers

from starlette.responses import JSONResponse
from justapi import JustAPIApp
app = JustAPIApp()
@app.get("/set-cookie")
def set_cookie():
response = JSONResponse(content={"message": "cookie set"})
response.set_cookie("session_id", "abc123", httponly=True, secure=True)
return response
@app.get("/clear-cookie")
def clear_cookie():
response = JSONResponse(content={"message": "cookie cleared"})
response.delete_cookie("session_id")
return response
Option Description
max_age Expiration in seconds
httponly Prevent JavaScript access
secure Only send over HTTPS
samesite CSRF protection ("lax", "strict", "none")
path Cookie scope path
@app.get("/with-headers")
def with_headers():
response = JSONResponse(content={"data": "value"})
response.headers["X-Request-Id"] = "req-123"
response.headers["X-Response-Time"] = "42ms"
return response