All posts

Senior Backend Interview: REST and APIs — Resources, Methods and Status Codes

What makes an API RESTful, idempotency, 401 vs 403, JWT vs sessions and versioning — all in sentences that sound like real experience.

Marijan Kopčić••2 min read
Senior Backend Interview: REST and APIs — Resources, Methods and Status Codes

REST questions aren't hard, but it is easy to start listing things and lose the point. The goal is to show in two sentences that you understand the model, not that you memorized a definition.

What makes an API RESTful

Resources are addressed by URI and they are nouns, not verbs — /reservations/12, not /getReservation. The HTTP method carries the meaning of the operation. Communication is stateless — the server remembers nothing between requests, every request carries everything it needs. The response is a representation of the resource, usually JSON, with the correct status code.

HTTP methods

  • GET — reads; it is safe and idempotent.
  • POST — creates; not idempotent, calling it twice creates two records.
  • PUT — replaces the whole resource; it is idempotent.
  • PATCH — changes part of it.
  • DELETE — deletes; idempotent, a second call doesn't change the state.

What idempotent means

That the same request sent several times leaves the system in the same state as if it had been sent once. It matters for retries after an error — otherwise a retry creates a duplicate reservation.

HATEOAS

The level at which a response also carries links to the next possible actions. In practice almost nobody does it — most APIs sit at level two of the Richardson Maturity Model, and that is usually perfectly fine.

Status codes

  • 200 — OK
  • 201 — created
  • 204 — OK, no content
  • 400 — bad request
  • 401 — you are not signed in
  • 403 — you are signed in, but not allowed
  • 404 — doesn't exist
  • 409 — conflict
  • 422 — validation failed
  • 429 — too many requests
  • 500 — server error
  • 503 — service unavailable

The difference between 401 and 403

401 means I don't know who you are, identify yourself. 403 means I know who you are, but you can't do this.

Authentication vs authorization

Authentication is who you are, authorization is what you are allowed to do. One happens first, then the other.

JWT vs sessions

A session keeps state on the server, the client only carries an ID. A JWT carries the data in the token itself, signed — the server doesn't have to remember anything, which is good for scaling. The downside of a JWT is that it is hard to revoke before it expires; you handle that with a short lifetime and a refresh token.

API versioning

Most often in the path, /v1/. It can also go in a header, but the path is more visible and easier to debug. I bump the version when I break compatibility, not for every change.

Keep reading