REST
REST is an architectural style for HTTP APIs: resources have URLs, verbs are HTTP methods, and representations (usually JSON) are transferred statelessly. Most "REST" APIs are pragmatic resource-oriented HTTP, not a dissertation on hypermedia.
Detailed Explanation
A resource is a thing you can name: /orders/11, /users/ada. You read it with GET, replace it with PUT, patch it with PATCH, delete it with DELETE, and create it with POST to a collection (/orders). The server does not store client session state between calls; each request carries auth and enough context to stand alone.
Representations are the bytes. JSON is the default in Python land. The same order can be application/json or text/csv if you honour Accept. Status codes are part of the interface: 201 with a Location header after create, 404 when the id is unknown, 409 when a conflicting update lands.
Idempotency matters. Retrying a GET is safe. Retrying a POST may create two orders unless you send an Idempotency-Key. PUT of the same body twice should leave the same resource. Design for retries; mobile networks fail.
Python frameworks map this to functions. Flask: @app.get("/orders/<id>"). FastAPI: @app.get("/orders/{id}") plus a Pydantic model. Django: class-based views or Django REST Framework viewsets. The framework is not REST. The URL and method design is.
Pure REST includes hypermedia (links in the body that tell the client what it can do next). Most teams skip that and publish an OpenAPI document instead. That is fine if you are honest about it. Do not call an RPC endpoint POST /rpc/GetOrder a REST API.
Versioning (/v1/orders) is a product choice. Compatibility is the real requirement: do not rename fields under a URL clients already store.
Code Examples
# FastAPI-shaped sketch of a resource.
from dataclasses import dataclass
@dataclass
class Order:
id: str
cents: int
ORDERS = {"11": Order("11", 2500)}
def get_order(order_id: str) -> tuple[int, dict]:
order = ORDERS.get(order_id)
if order is None:
return 404, {"error": "not_found"}
return 200, {"id": order.id, "cents": order.cents}
print(get_order("11"), get_order("0"))Common Gotchas
`GET /orders/delete?id=1`
the method is wrong and caches may replay it. Use `DELETE /orders/1`.
POST that is not idempotent, retried by a client
duplicate charges. Accept an idempotency key.
200 + error payload
load balancers and caches treat it as success. Use 4xx/5xx.
Session cookies plus CSRF is not the same as a bearer token API
pick one auth story per client type.