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