Designing Production HTTP 200 OK Responses Without Cache Leaks
Because RFC 9110 defines 200 OK as heuristically cacheable by default, omitting Cache-Control on authenticated API responses can allow shared proxies or CDNs to cache user-specific data if a Last-Modified header is present. Always declare Cache-Control: private, no-store on personalized JSON endpoints and pair public read endpoints with explicit max-age and ETag validators so clients can negotiate 304 Not Modified on subsequent polls.
For clean REST semantics, reserve 200 OK for successful reads and mutations that return a meaningful representation. When an endpoint creates a brand-new resource with a distinct URI, prefer 201 Created with a Location header; when a DELETE or PUT succeeds without needing to echo back bytes, return 204 No Content.
- RFC SpecificationRFC 9110 §15.3.1 (Successful 2xx)
- Default CacheabilityHeuristically cacheable by default unless Cache-Control restricts it
- Idempotency & RetriesTerminal success — do not retry
When should I return 200 OK vs 201 Created vs 204 No Content?
Use 200 OK when returning a resource or action result body, 201 Created when a POST creates a new server-assigned resource (with a Location header), and 204 No Content when a mutation succeeds with no body.
Is an HTTP 200 response cached by browsers automatically?
Yes, GET responses with 200 OK are heuristically cacheable if Last-Modified is sent without Cache-Control. Set Cache-Control: no-store for sensitive data or explicit max-age values for deterministic CDN caching.
Why is returning {"error": true} with HTTP 200 considered an API anti-pattern?
Returning 200 on failures prevents HTTP monitoring, load balancers, browser fetch checks (res.ok), and API gateways from distinguishing healthy responses from broken ones, and risks caching error payloads in CDNs.