Request Links Example (Phase 7)
Output fields bound to the current request: FromHeader("X-Request-Id") and FromCookie("session_id"). Responses echo headers and cookies when present.
Code
from typing import Annotated
from pydantic import BaseModel
from semblance import FromCookie, FromHeader, FromInput, SemblanceAPI
class UserQuery(BaseModel):
name: str = "alice"
class UserResponse(BaseModel):
name: Annotated[str, FromInput("name")]
request_id: Annotated[str, FromHeader("X-Request-Id")]
session: Annotated[str, FromCookie("session_id")]
api = SemblanceAPI(seed=42)
@api.get(
"/user",
input=UserQuery,
output=UserResponse,
summary="Get user (echoes header and cookie)",
)
def get_user():
"""request_id comes from X-Request-Id header; session from session_id cookie."""
pass
app = api.as_fastapi()
Run
semblance run examples.request_links.app:api --port 8000
Try
# With header and cookie
curl -i "http://127.0.0.1:8000/user?name=alice" \
-H "X-Request-Id: req-abc-123" \
-H "Cookie: session_id=sess-xyz"
# Response includes "request_id": "req-abc-123", "session": "sess-xyz"
# If header/cookie missing, those fields get generated values.
Example response with -H "X-Request-Id: id-1" -H "Cookie: session_id=sess-1" and ?name=alice (seed=42):
{"name": "alice", "request_id": "id-1", "session": "sess-1"}
Concepts
- Phase 7 — Built-in request links:
FromHeader(name)andFromCookie(name). - Resolved at response build time from the current request; works with
test_clientand live servers. - See Request Links for details.