Setting Cookies
from justapi 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
Deleting Cookies
@app.get("/clear-cookie")
def clear_cookie():
response = JSONResponse(content={"message": "cookie cleared"})
response.delete_cookie("session_id")
return response
Cookie Options
| 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 |
Custom Response Headers
@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
See Also
- Return a Response Directly — full response control
- Security — First Steps — auth with cookies
- Cookie Parameters — reading cookies