Introduction to REST
REST (Representational State Transfer) is a set of conventions for designing web APIs.
Core idea: resources
Section titled “Core idea: resources”A REST API is organized around resources:
/users/posts/orders
You use HTTP methods to operate on them:
- GET
/users→ list users - GET
/users/1→ get one user - POST
/users→ create user - PUT/PATCH
/users/1→ update - DELETE
/users/1→ delete
Status codes matter
Section titled “Status codes matter”- 200 OK — success
- 201 Created — created successfully
- 400 Bad Request — invalid request data
- 401 Unauthorized — missing/invalid auth
- 403 Forbidden — authenticated but not allowed
- 404 Not Found — resource doesn’t exist
Statelessness
Section titled “Statelessness”REST APIs are typically stateless:
- every request contains everything needed (auth token, parameters)
That’s why token-based auth (JWT) is common.
A good REST mental model
Section titled “A good REST mental model”flowchart LR Client -->|HTTP + JSON| API[Flask API] API -->|ORM| DB["(Database)"] DB --> API API -->|JSON + status| Client
Practical tip
Section titled “Practical tip”REST is a guideline, not a law.
Aim for:
- consistency
- clear error responses
- predictable URLs
Visualize it
Section titled “Visualize it”Here’s how the standard HTTP methods map onto CRUD operations for a single resource.
flowchart LR GET["GET"] --> Read["Read"] POST["POST"] --> Create["Create"] PUT["PUT/PATCH"] --> Update["Update"] DELETE["DELETE"] --> Delete["Delete"] Read --> R["/api/tasks"] Create --> R Update --> R Delete --> R
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – Create a Flask App
Section titled “Exercise 1 – Create a Flask App”Exercise 2 – Dynamic Route
Section titled “Exercise 2 – Dynamic Route”Exercise 3 – Return JSON
Section titled “Exercise 3 – Return JSON”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading