Concept Explanation
When your frontend talks to your backend, they need a shared language. REST is the most common set of conventions for that conversation. The core idea: everything is a resource (a user, a post, an order), each resource has a URL, and you act on it using standard HTTP verbs.
Think of a library. The books are resources. You don't invent a new door for every action — you use the same doors (GET to read, POST to add, DELETE to remove) on different shelves (URLs).
Syntax or Core Structure
Use nouns for URLs and verbs for actions:
GET /users list all users
GET /users/42 get user 42
POST /users create a new user
PUT /users/42 replace user 42
PATCH /users/42 update part of user 42
DELETE /users/42 delete user 42
Return the right status code so the client knows what happened:
200 OK 201 Created 204 No Content
400 Bad Request 401 Unauthorized 404 Not Found
500 Server Error
Common Mistakes
- Verbs in URLs:
/getUseror/createUserNowis wrong. The verb is the HTTP method; the URL is a noun. - Always Returning 200: Sending
200 OKwith{ error: "not found" }inside confuses clients. Use the real status code. - Inconsistent Names: Mixing
/userand/usersmakes the API hard to learn. Pick plural nouns and stick to them.
Best Practices
- Plural Nouns:
/products,/orders— consistency beats cleverness. - Nest Relationships:
/users/42/ordersreads as "orders belonging to user 42." - Version Your API: Prefix routes with
/v1/so you can evolve without breaking old clients.
Practice Exercises
- The Blueprint: Design the six REST routes for a "todos" resource.
- The Status Picker: For each action (create, missing item, bad input), name the correct status code.
- The Fixer: Rewrite
/deletePostById?id=5as a proper RESTful route.