adapters-nethttp-client

command
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 17 Imported by: 0

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:

  1. 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
  2. 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
  3. Cookies + headers — GET /profile (CookieParam + HeaderParam validation)
  4. Security — GET /data (CredentialFunc injects bearer Authorization header)
  5. Structured error logging — errors.As + slog for all typed error types

Run with: go run ./examples/adapters-nethttp-client

Directories

Path Synopsis
Package contract defines the shared HTTP API contract for the adapters-nethttp-client example.
Package contract defines the shared HTTP API contract for the adapters-nethttp-client example.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL