Why must a CONTEXT-bound service parameter be typed HTTPContext in EK9?
← Web Services · Ref: Q1329
The CONTEXT qualifier gives a service operation access to the request context (authenticated principal, security state, connection metadata), so the bound parameter MUST be typed HTTPContext. Binding :=: CONTEXT to a String (or any other type) triggers E07791 because the framework supplies a context object, not a parsed value.
CORRECT PATTERN
info() as GET for :/data -> ctx as HTTPContext :=: CONTEXT
INCORRECT PATTERN
info() as GET for :/data -> ctx as String :=: CONTEXT
Do not confuse CONTENT with CONTEXT. CONTENT binds the request body, which IS a String; CONTEXT binds request metadata, which is an HTTPContext. For individual request values use the specific access qualifiers (:=: QUERY, :=: PATH).
See Q202 for parameter binding. See Q868 for parameter type rules.
Example
defines module qa.webdeep.service.context.binding defines constant JSON_RESPONSE <- "application/json" defines service <?- Service using CONTEXT binding correctly: the parameter is typed HTTPContext, which is what the framework supplies for :=: CONTEXT. -?> InfoService :/info open info() as GET for :/data -> ctx as HTTPContext :=: CONTEXT <- response as HTTPResponse: (capturedCtx: ctx) with trait HTTPResponse override content() <- rtn as String: `{"hasContext": ${capturedCtx?}}` override status() as pure <- rtn as Integer: 200 override contentType() as pure <- rtn as String: JSON_RESPONSE override cacheControl() as pure <- rtn as String: "no-cache" override contentLanguage() as pure <- rtn as String: "en" default operator ? defines application InfoApp register InfoService() defines program ContextBindingDemo() stdout <- Stdout() stdout.println("CONTEXT binds an HTTPContext (request metadata).") stdout.println("CONTENT binds a String (the request body).")
Common mistakes
E07791 — The CONTEXT qualifier supplies the request context object (principal, security state, connection metadata), so the parameter must be typed HTTPContext. Binding :=: CONTEXT to a String triggers E07791. CONTENT binds the body (a String); CONTEXT binds metadata (an HTTPContext) — do not confuse them. See ek9 -h E07791 for details.
Incorrect:
-> ctx as String :=: CONTEXT
Correct:
-> ctx as HTTPContext :=: CONTEXT
Other ways to ask this
- What triggers E07791 wrong type for CONTEXT binding?
- Why can't I bind :=: CONTEXT to a String parameter in an EK9 service?
- What is the difference between CONTENT and CONTEXT binding in EK9 services?
Coming from another language?
Java Spring: SecurityContextHolder / Principal injected as method args, validated only at runtime. C# ASP.NET: HttpContext available ambiently, no compile-time type checking of binding. Go: context.Context passed manually with no framework guard. EK9: the compiler enforces at compile time that a CONTEXT-bound parameter is exactly HTTPContext, eliminating mismatched-binding bugs before the service runs.
Keywords: HTTPContext, binding, service, E07791, CONTEXT, request, type, CONTENT