Status Codes
16 min
Beyond the broad category from the HTTP course (2xx/3xx/4xx/5xx), REST APIs use SPECIFIC codes within each category to communicate precisely what happened — picking the right one is part of designing a good API, not just an afterthought.
def status_category(code):
if 100 <= code < 200: return "informational"
if 200 <= code < 300: return "success"
if 300 <= code < 400: return "redirection"
if 400 <= code < 500: return "client_error"
if 500 <= code < 600: return "server_error"
return "unknown"
401 vs 403 is a common point of confusion: 401 means 'we don't know who you are' (log in), 403 means 'we know who you are, and you're not allowed' (permission denied) -- genuinely different situations that call for different client behavior.
Choosing the right code for each operation makes an API predictable and self-documenting — a client can react correctly to a status code alone, without parsing the response body:
def choose_status_for_operation(operation, success):
mapping = {
("create", True): 201, ("create", False): 400,
("delete", True): 204, ("delete", False): 404,
("read", True): 200, ("read", False): 404,
}
return mapping.get((operation, success), 500)
A common mistake: returning 200 OK for EVERYTHING, with the real
success/failure info buried in the response body. That forces every
client to parse the body just to know if something worked — status
codes exist precisely so that layer of information doesn't require
parsing anything.
Write `status_category(code)`: return `'informational'`, `'success'`, `'redirection'`, `'client_error'`, `'server_error'`, or `'unknown'` based on which hundred-range `code` falls in.
Write `choose_status_for_operation(operation, success)`: return the conventional status code for `('create', True)`→201, `('create', False)`→400, `('delete', True)`→204, `('delete', False)`→404, `('read', True)`→200, `('read', False)`→404. Return 500 for anything else.
What status code should a successful POST that creates a new resource typically return?
You can categorize status codes and pick the conventionally correct one for common CRUD operations.