Now you will create, view, and cancel orders. These endpoints require authentication, so have your Bearer token ready.
Create an order with one or more products and a shipping address.
curl -X POST BASE_URL/orders \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{
"items": [
{ "productId": "prod-1", "quantity": 2 },
{ "productId": "prod-5", "quantity": 1 }
],
"shippingAddress": {
"street": "123 Main Street",
"city": "Mumbai",
"state": "Maharashtra",
"zipCode": "400001",
"country": "India"
}
}'Expected response (201 Created):
{
"success": true,
"data": {
"id": "ord-9",
"userId": "usr-6",
"items": [
{ "productId": "prod-1", "productName": "Wireless Bluetooth Headphones",
"quantity": 2, "unitPrice": 2499, "subtotal": 4998 },
{ "productId": "prod-5", "productName": "Running Shoes Pro",
"quantity": 1, "unitPrice": 3499, "subtotal": 3499 }
],
"totalAmount": 8497,
"status": "pending"
}
}Save the order ID (e.g., ord-9) from the response. You will need it to view or cancel the order.
curl BASE_URL/orders \
-H "Authorization: Bearer YOUR_TOKEN"To get a specific order, append the order ID:
curl BASE_URL/orders/YOUR_ORDER_ID \
-H "Authorization: Bearer YOUR_TOKEN"Cancel a pending order using the DELETE method. Only orders with status pending can be cancelled.
curl -X DELETE BASE_URL/orders/YOUR_ORDER_ID \
-H "Authorization: Bearer YOUR_TOKEN"A successful delete returns 204 No Content — no response body. This is standard REST practice for delete operations.
After cancelling, run GET BASE_URL/orders/YOUR_ORDER_ID again. The status should now be cancelled.
1. Create a new order, verify it appears in your orders list, then cancel it and confirm the status changed to cancelled.
2. Try creating an order with a product ID that does not exist (e.g., prod-999). What error do you get?
3. Try cancelling an order that is already cancelled. What happens?
Q: Why does a DELETE request return 204 No Content instead of 200 OK?
A: HTTP 204 No Content means the request succeeded but there is no response body to return. It is the standard status code for DELETE operations because the resource has been removed — there is nothing left to send back. Some APIs return 200 with a confirmation message instead, but 204 is considered more RESTful.