🔥 0
0

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

CRUDHTTP MethodE-commerce Example
CreatePOSTAdd item to cart, place order
ReadGETView products, check order status
UpdatePUT / PATCHChange quantity, update address
DeleteDELETERemove 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
Response (201 Created):
{
  "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 id assigned 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

AspectPUTPATCH
What it sendsEntire resourceOnly changed fields
Missing fieldsMay be deleted/resetUnchanged
Use caseFull replacementPartial update
ExampleReplace entire profileChange just email
Real-world example:

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

ScenarioMethodWhat to Test
Add duplicate itemPOSTShould update qty, not create new
Update non-existentPUTShould return 404
Delete already deletedDELETEShould return 404 or 204
Create with missing fieldsPOSTShould return 400 with validation error
Negative quantityPATCHShould reject invalid data

---

Key Takeaways

MethodPurposeHas Body?Idempotent?
GETReadNoYes
POSTCreateYesNo
PUTReplaceYesYes
PATCHPartial updateYesYes
DELETERemoveNoYes
Idempotent = Running multiple times has same effect as running once.

Next up: Request Parameters & Headers - passing data and authentication!

Your First API Request