Reviewer · API
Consistent endpoint semantics
Keep HTTP endpoint behavior predictable and standards-compliant across all request variants of the same resource. 1. Method handling: register every method an endpoint logically supports, and use standard HTTP semantics to reject disallowed variants instead of silently omitting the route (which produces a confusing 404). For example, when a GET alias for a...
Keep HTTP endpoint behavior predictable and standards-compliant across all request variants of the same resource.
- Method handling: register every method an endpoint logically supports, and use standard HTTP semantics to reject disallowed variants instead of silently omitting the route (which produces a confusing 404). For example, when a GET alias for a state-changing action is feature-flagged, always register GET and POST, and wrap the handler so disabled GET requests get
405 Method Not Allowedwith anAllowheader, while POST passes through unconditionally:
func rejectGETUnlessAllowed(allowGET bool) middleware.Func {
return func(next http.Handler) http.Handler {
if allowGET {
return next
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
w.Header().Set("Allow", http.MethodPost)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
next.ServeHTTP(w, r)
})
}
}
-
Response format: the shape and encoding of a response must not change based on optional query parameters or flags unless explicitly versioned/documented. If the base endpoint returns YAML, a filtered/partial view of the same resource (e.g. via a
?q=query parameter) should also return YAML, not switch to JSON; introduce an explicitformat/Accept-based parameter if multiple encodings are genuinely needed. -
Handler interfaces: when registering HTTP routes for a component, depend on that component’s existing public interface (e.g.
ingester.Interface) rather than re-declaring a narrower duplicate interface just for the HTTP layer—this avoids drift between the two definitions and simplifies test fakes.
Apply these rules whenever adding or modifying endpoints, feature-flagged behavior, or alternate response views, and cover the method-rejection and format-consistency cases with explicit tests (e.g. asserting the exact 405 + Allow header, or that both response paths use the same encoding).