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 URLActionSuccess
GET /productsList products (filter, page)200
GET /products/:idOne product200 / 404
POST /productsCreate201
PATCH /products/:idUpdate some fields200
DELETE /products/:idDelete204

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