HTTP Methods: CRUD Operations
In e-commerce, you need to Create orders, Read products, Update cart quantities, and Delete items. Each action uses a different HTTP method.
The CRUD-HTTP Mapping
| CRUD | HTTP Method | E-commerce Example |
|---|---|---|
| Create | POST | Add item to cart, place order |
| Read | GET | View products, check order status |
| Update | PUT / PATCH | Change quantity, update address |
| Delete | DELETE | Remove from cart, cancel order |
---
GET - Read Data
We covered GET in the previous lesson. Quick recap:
GET /api/products → List all products
GET /api/products/123 → Get specific product
GET /api/products?category=electronics → Filter
Characteristics:
- ✅ Safe - doesn't modify data
- ✅ Cacheable - browsers can cache responses
- ✅ No request body
---
POST - Create Data
POST creates new resources. Let's add an item to a cart:
Adding to Cart
- New request → Select POST
- URL:
https://fakestoreapi.com/carts - Go to Body tab
- Select raw and JSON
- Enter:
{
"userId": 1,
"date": "2024-01-15",
"products": [
{ "productId": 5, "quantity": 2 },
{ "productId": 1, "quantity": 3 }
]
}
- Click Send
{
"id": 21,
"userId": 1,
"date": "2024-01-15",
"products": [
{ "productId": 5, "quantity": 2 },
{ "productId": 1, "quantity": 3 }
]
}
Notice:
- Status is
201 Created(not 200) - Response includes the new
idassigned by the server
Creating a User Account
POST https://fakestoreapi.com/users
Body:
{
"email": "john@example.com",
"username": "johndoe",
"password": "secure123",
"name": {
"firstname": "John",
"lastname": "Doe"
},
"address": {
"city": "San Francisco",
"street": "123 Main St",
"zipcode": "94102"
},
"phone": "1-555-123-4567"
}
---
PUT - Replace Entire Resource
PUT replaces the entire resource. If you omit a field, it may be deleted.
Update Entire Cart
PUT https://fakestoreapi.com/carts/7
Body:
{
"userId": 1,
"date": "2024-01-16",
"products": [
{ "productId": 3, "quantity": 1 }
]
}
This replaces the entire cart - previous items are gone!
Response (200 OK):{
"id": 7,
"userId": 1,
"date": "2024-01-16",
"products": [
{ "productId": 3, "quantity": 1 }
]
}
Update User Profile
PUT https://fakestoreapi.com/users/1
Body:
{
"email": "newemail@example.com",
"username": "johndoe",
"password": "newpassword",
"name": {
"firstname": "John",
"lastname": "Smith"
},
"address": {
"city": "Los Angeles",
"street": "456 Oak Ave",
"zipcode": "90001"
},
"phone": "1-555-987-6543"
}
---
PATCH - Partial Update
PATCH updates only the fields you send - other fields remain unchanged.
Update Just the Quantity
PATCH https://fakestoreapi.com/carts/7
Body (only what's changing):
{
"products": [
{ "productId": 3, "quantity": 5 }
]
}
Other cart fields (userId, date) stay the same.
PUT vs PATCH
| Aspect | PUT | PATCH |
|---|---|---|
| What it sends | Entire resource | Only changed fields |
| Missing fields | May be deleted/reset | Unchanged |
| Use case | Full replacement | Partial update |
| Example | Replace entire profile | Change just email |
Updating shipping address:
- PUT: Send entire order object (items, payment, shipping)
- PATCH: Send just
{ "shippingAddress": {...} }
---
DELETE - Remove Data
DELETE removes a resource.
Remove from Cart
DELETE https://fakestoreapi.com/carts/7
No body needed!
Response (200 OK):{
"id": 7,
"userId": 1,
"date": "...",
"products": [...]
}
Many APIs return the deleted resource for confirmation.
Delete User Account
DELETE https://fakestoreapi.com/users/1
---
Complete E-commerce Flow
Let's walk through a typical shopping flow:
1. Browse Products (GET)
GET /api/products?category=electronics
2. Add to Cart (POST)
POST /api/carts
{
"userId": 1,
"products": [{ "productId": 5, "quantity": 1 }]
}
3. Update Quantity (PATCH)
PATCH /api/carts/21
{
"products": [{ "productId": 5, "quantity": 3 }]
}
4. Remove Item (DELETE)
DELETE /api/carts/21/items/5
5. Place Order (POST)
POST /api/orders
{
"cartId": 21,
"paymentMethod": "card",
"shippingAddress": {...}
}
6. Check Order Status (GET)
GET /api/orders/99
---
Practice Exercises
Exercise 1: Create a Product (POST)
POST https://fakestoreapi.com/products
Body:
{
"title": "Wireless Bluetooth Headphones",
"price": 79.99,
"description": "High-quality wireless headphones with noise cancellation",
"image": "https://example.com/headphones.jpg",
"category": "electronics"
}
Save as: Create Product in Products folder
Exercise 2: Update Product Price (PATCH)
PATCH https://fakestoreapi.com/products/7
Body:
{
"price": 59.99
}
Save as: Update Product Price
Exercise 3: Delete a Product (DELETE)
DELETE https://fakestoreapi.com/products/7
Save as: Delete Product
---
Common Testing Scenarios
| Scenario | Method | What to Test |
|---|---|---|
| Add duplicate item | POST | Should update qty, not create new |
| Update non-existent | PUT | Should return 404 |
| Delete already deleted | DELETE | Should return 404 or 204 |
| Create with missing fields | POST | Should return 400 with validation error |
| Negative quantity | PATCH | Should reject invalid data |
---
Key Takeaways
| Method | Purpose | Has Body? | Idempotent? |
|---|---|---|---|
| GET | Read | No | Yes |
| POST | Create | Yes | No |
| PUT | Replace | Yes | Yes |
| PATCH | Partial update | Yes | Yes |
| DELETE | Remove | No | Yes |
Next up: Request Parameters & Headers - passing data and authentication!