Service HTTP request and response contract
Know where to send arguments and how to handle errors, files, and streams before wiring a client.
For a JSON endpoint, handle errors by HTTP status and error.code, and read success data from data. Files, redirects, and streams use their own response types. In-process calls share handlers and return result objects with headers.
Where to send request input
| Input | Controller reads | Transport behavior |
|---|---|---|
| URL query parameters | ctx.query |
Repeated parameters retain occurrence order. Use s.single for single-value fields |
| JSON or form fields | ctx.body |
Supply the appropriate Content-Type. Ordinary GET and HEAD skip the request body |
| Multipart files | ctx.files |
Text fields stay in the body. The parser allows up to 16 MiB and 32 parts, with no more than 1 MiB of text |
| Requests that need raw bytes | ctx.rawBody |
Declare rawBody: true for the route so the handler receives bytes |
JSON, text, URL-encoded forms, and raw bytes have a 1 MiB read limit. A nonempty body with an unknown media type can reach the handler as a stream. Controllers call ctx.parse to validate query, body, or files explicitly. Validation errors identify the request part and list field issues.
How to consume a response
- A successful JSON response contains
data,error: null, andmeta. For failure,datais null. Use the status and code, and correlatemeta.requestIdwith theX-Request-Idheader. - For 404
route_not_found, trace the route. For 405method_not_allowed, inspecterror.details.allow. For 403origin_forbidden, check the browser’s Origin and the allowed-origin configuration. - Consume files and text by their media type. Read
Locationfor redirects and the status for empty responses. HEAD returns headers and status without a body. - Parse NDJSON one event per line. A failure can arrive after data starts streaming. Handle a
type: "error"event using its error code. For a complete result, wait for the completion event.
HTTP versus in-process invocation
The HTTP gateway parses bodies and converts handler results into HTTP responses. An in-process call supplies prepared arguments to runtime.call and receives { result, headers }. A handler failure throws ServiceFailure. When tracing a route used both ways, check argument shape and error handling at each boundary.
Service HTTP tests cover form and multipart parsing, raw bytes, JSON envelopes, and NDJSON stream errors. Verification report · Routing · Controllers