Skip to content

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

  • GET is safe and cacheable: no state change, no side effect that matters.
  • PUT replaces a representation at a known URI and is idempotent.
  • PATCH applies a partial change; idempotency depends on the patch format.
  • POST creates or processes according to the target resource; no automatic idempotency.
  • DELETE removes 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:

  • 200 for a successful response body, 201 when a new resource was created, 202 when work is accepted but not finished, 204 when success has no body.
  • 400 for malformed input, 401 for missing or invalid authentication, 403 for authenticated but forbidden, 404 when the resource is absent or deliberately hidden, 409 for state conflicts, 412 for failed preconditions, 422 for well-formed input that violates domain rules, 429 for rate limiting.
  • 500 for your fault, 503 when temporarily unavailable, preferably with Retry-After.
HTTP/1.1 303 See Other
Location: /payment-attempts/pa_82Qx

Redirects 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 GET that 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.
  • PUT is not "update" in the casual sense; it means replacement. If clients cannot send complete state, use PATCH.
  • Correct 405 plus an Allow header is kinder than a confusing 404 when the collection exists but the method does not.

Was this lesson helpful?

Sign in to rate