Documentation
¶
Overview ¶
Package adapters-nethttp-client demonstrates the HTTP client-side adapter.
go-codex routes are declarative values — the same rest.Route that drives the server (nethttp.Handler / nethttp.Register) can be used on the client (nethttp.Call) to make typed HTTP calls with full codec validation.
The shared contract/ sub-package defines types, codecs, and route specs. Both server and client import it. The Go compiler enforces the contract: any change breaks compilation on both sides immediately — no stale YAML.
The example covers all CallOptions fields in five sections:
- Body — POST /users (request body codec, shared-contract pattern) 1b. Client-side typed error decode — nethttp.Call returns a decoded nethttp.ErrorPatternResponse (instead of the untyped UnexpectedStatusError) when the response status matches a rest.ErrorPattern declared on the route — errors.As extracts the typed payload directly, no manual status-switch or json.Unmarshal
- Path params — GET /users/{id} (codec-validated path variable) 2b. Client encode with role-aware merge fields — GET /users/{id}/activity (rest.NewPathParam/NewOptionalQueryParam + codex.EncodeVars via RouteHandle.PathMergeFields()/QueryMergeFields(), each scoped to its own HTTP location so values never leak across roles) 2c. One-line client call — nethttp.CallHandle derives every request map from the SAME req automatically, and decodes a response header merge field (rest.NewRequiredResponseHeaderParam) straight into Resp — the full request+response, single-call story for one route
- Cookies + headers — GET /profile (CookieParam + HeaderParam validation)
- Security — GET /data (CredentialFunc injects bearer Authorization header)
- Structured error logging — errors.As + slog for all typed error types
Run with: go run ./examples/adapters-nethttp-client
Click to show internal directories.
Click to hide internal directories.