Careeroza
HTTP Methods
The Five Methods You Will Use
| Method | Meaning | ShopEase example | Has body? | Safe | Idempotent |
|---|---|---|---|---|---|
GET | read data | GET /api/products | no | yes | yes |
POST | create a new resource | POST /api/orders | yes | no | no |
PUT | replace a resource completely | PUT /api/products/5 | yes | no | yes |
PATCH | update some fields | PATCH /api/products/5 (only stock) | yes | no | not guaranteed |
DELETE | remove a resource | DELETE /api/cart/7 | usually no | no | yes |
- Safe: does not change data on the server (only reads).
- Idempotent: sending the same request once or ten times leaves the server in the same state. Important when a mobile app retries after a network error.
Idempotency in Action
Example
Simulating a product store: repeating a PUT gives the same result, repeating a POST creates duplicates.
import java.util.Map;
import java.util.TreeMap;
public class Main {
static Map<Integer, String> products = new TreeMap<>();
static int nextId = 1;
static void post(String name) { products.put(nextId++, name); } // create
static void put(int id, String name) { products.put(id, name); } // replace
static void delete(int id) { products.remove(id); } // remove
public static void main(String[] args) {
post("Boat Rockerz 450");
post("Boat Rockerz 450"); // retried POST -> duplicate!
System.out.println("After 2 x POST : " + products);
put(1, "Boat Rockerz 550");
put(1, "Boat Rockerz 550"); // retried PUT -> same state
System.out.println("After 2 x PUT : " + products);
delete(2);
delete(2); // retried DELETE -> same state
System.out.println("After 2 x DELETE: " + products);
}
}Output
After 2 x POST : {1=Boat Rockerz 450, 2=Boat Rockerz 450}
After 2 x PUT : {1=Boat Rockerz 550, 2=Boat Rockerz 450}
After 2 x DELETE: {1=Boat Rockerz 550}Tip: Payment and order APIs (Paytm, Razorpay) protect POST with an idempotency key header so that a retry does not charge the customer twice.