Careeroza
REST Principles
Designing Good URLs
- Use nouns, not verbs:
/products, not/getProducts. - Use plural collection names:
/products/7. - Nest for ownership:
/orders/42/items. - Use query parameters for filters and paging:
/products?category=audio&page=2. - Let the method say the action:
DELETE /products/7. - Stateless: each request carries everything needed (for example the token). The server keeps no session in memory.
The ShopEase Product API
| Method and URL | Action | Success |
|---|---|---|
GET /products | List products (filter, page) | 200 |
GET /products/:id | One product | 200 / 404 |
POST /products | Create | 201 |
PATCH /products/:id | Update some fields | 200 |
DELETE /products/:id | Delete | 204 |
Exercise: Design the URLs and methods for a cart: view my cart, add an item, change quantity of an item, remove an item, empty the cart.
Show answer
GET /cart view my cart
POST /cart/items add an item body: { productId, quantity }
PATCH /cart/items/:itemId change quantity body: { quantity }
DELETE /cart/items/:itemId remove an item
DELETE /cart empty the cart