Lesson 2 of 9 · 22m
HTTP Semantics: Methods and Status Codes Mean Something
Use GET, PUT, PATCH, POST, DELETE, and status codes as contracts so caches, retries, and clients behave correctly by default.
HTTP is not just a transport envelope for JSON. Its methods and status codes are instructions to every intermediary in the chain: browsers, proxies, CDNs, load balancers, mobile SDKs, and retry logic you did not write. APIs that respect those instructions get correct behavior for free; APIs that treat everything as POST /rpc have to reimplement the platform badly.
Methods carry safety and idempotency promises
GETis safe and cacheable: no state change, no side effect that matters.PUTreplaces a representation at a known URI and is idempotent.PATCHapplies a partial change; idempotency depends on the patch format.POSTcreates or processes according to the target resource; no automatic idempotency.DELETEremoves the resource and is idempotent in effect even if the second response is 404.
PATCH /invoices/inv_9Zk2 HTTP/1.1
Content-Type: application/merge-patch+json
{"po_number": "PO-48211", "metadata": {"cost_center": "infra"}}If merging that patch twice has the same result, clients can retry. If it appends to a list, they cannot. Semantics are implementation facts, not documentation decoration.
Status codes are for machines first
Use a narrow set consistently:
200for a successful response body,201when a new resource was created,202when work is accepted but not finished,204when success has no body.400for malformed input,401for missing or invalid authentication,403for authenticated but forbidden,404when the resource is absent or deliberately hidden,409for state conflicts,412for failed preconditions,422for well-formed input that violates domain rules,429for rate limiting.500for your fault,503when temporarily unavailable, preferably withRetry-After.
HTTP/1.1 303 See Other
Location: /payment-attempts/pa_82QxRedirects are part of semantics too. 201 plus Location tells a create client where the object landed; 303 after processing tells a browser not to resubmit on refresh.
Do not tunnel errors through 200
HTTP 200 with { "success": false } breaks every generic piece of tooling: retries stay silent, monitoring says everything is healthy, caches store failures, and client SDKs need bespoke branching. Transport success and business outcome are separate layers. If the request failed, the status code should say so.
Content negotiation is smaller than people think
You do not need to support ten media types. You do need to version deliberately if you use a custom media type, honor Accept when you claim multiple representations, and send accurate Content-Type. For most modern APIs: application/json for resources, application/problem+json for errors, and binary formats only where they truly belong.
Field notes
- Safe does not mean free: a
GETthat triggers an expensive report build is still a liability. - Never mutate state in
GET, even for conveniences like tracking a download; use a separate event ingestion endpoint. PUTis not "update" in the casual sense; it means replacement. If clients cannot send complete state, usePATCH.- Correct
405plus anAllowheader is kinder than a confusing 404 when the collection exists but the method does not.
Was this lesson helpful?