HTTP Status Codes
When we develop a backend web service, we often need to decide which HTTP status code to return. It can be a chore and yet it's part of the contract that you don't want to get it wrong or inconsistent.
Most of the time, you only need to consider if the request being successful or not.
If it succeeded:
- 201 — created a resource. e.g. created a user, a project, a record, an event
- 200 — other successes
If it failed:
- 401 — issue with authentication or login. e.g. failed to login or user performs an action and we check that the user session is no longer/not valid
- 400 — other errors
Usually the framework or library we are using will return other more specific status codes like 429 (rate limited), 405 (wrong/invalid method, like GET instead of POST or vice versa). It can be that simple.