HTTP & REST APIs

HTTP Methods & Status Codes

~310 words · 2 min read

HTTP in one sentence

HTTP is a request–response protocol: a client sends a request, the server replies with a response. Every request carries a method (what to do) and every response carries a status code (how it went).

The main methods

  • GET — read a resource. Should never change server state.
  • POST — create a new resource. Not idempotent.
  • PUT — replace an entire resource at a known URL.
  • PATCH — partially update a resource (just the changed fields).
  • DELETE — remove a resource.

Safe vs idempotent

Two properties govern method behaviour:

  • Safe — no server state change (GET, HEAD).
  • Idempotent — repeating the same request has the same effect as doing it once (GET, PUT, DELETE). POST and PATCH are not.

Status code families

  • 2xx — success. 200 OK, 201 Created, 204 No Content.
  • 3xx — redirection. 301 Moved Permanently.
  • 4xx — client error. 400 Bad Request, 401 Unauthorized, 404 Not Found, 422 Unprocessable.
  • 5xx — server error. 500 Internal Server Error, 503 Service Unavailable.

PUT vs PATCH

PUT /users/42 sends the full representation and replaces it. PATCH /users/42 sends only the fields that change:

PATCH /users/42
{ "email": "new@mail.com" }
A status code is a contract. Returning 200 with an error body, or 201 when nothing was created, breaks every client that relies on standard semantics.