HTTP Methods

The Five Methods You Will Use

MethodMeaningShopEase exampleHas body?SafeIdempotent
GETread dataGET /api/productsnoyesyes
POSTcreate a new resourcePOST /api/ordersyesnono
PUTreplace a resource completelyPUT /api/products/5yesnoyes
PATCHupdate some fieldsPATCH /api/products/5 (only stock)yesnonot guaranteed
DELETEremove a resourceDELETE /api/cart/7usually nonoyes
  • 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.