HTTP Methods

Use GET, POST, PUT, PATCH, DELETE, and idempotency correctly in an API.

Lesson content

🎯 Goal By the end of this lesson, you'll understand: What are HTTP Methods? Why do we use them? Common HTTP Methods REST API examples Safe vs Idempotent methods Real-world examples Interview questions What are HTTP Methods? HTTP Methods (also called HTTP Verbs ) define what action a client wants to perform on a resource . Simple Definition: HTTP Methods tell the server what to do with the requested resource. Why Do We Need HTTP Methods? When a client sends a request to the server, it must specify the operation. For example: Get user details Create a new user Update a user Delete a user HTTP Methods make these operations clear. Real-Life Example 📚 Imagine a library. Library │ ├── Read Book ├── Add Book ├── Update Book └── Remove Book Similarly, HTTP Methods perform operations on resources. Common HTTP Methods Method Purpose GET Retrieve data POST Create new data PUT Replace existing data PATCH Partially update data DELETE Remove data HEAD Get headers only OPTIONS Get supported methods 1. GET Used to retrieve data . Example GET /users Response [ { "id": 1 , "name": "Ajay" } ] 2. POST Used to create a new resource . Example POST /users Request Body { "name": "Ajay" , "email": "ajay@example.com" } Response 201 Created 3. PUT Used to replace an entire resource . Example PUT /users/1 Request Body { "name": "Ajay Singh" , "email": "ajay@example.com" } The entire user resource is replaced with the new data. 4. PATCH Used to update only specific fields . Example PATCH /users/1 Request Body { "email": "new@example.com" } Only the email is updated. 5. DELETE Used to remove a resource . Example DELETE /users/1 Response 204 No Content 6. HEAD Works like GET , but returns only response headers , not the response body. Example HEAD /users Response 200 OK Content-Type: application/json Content-Length: 150 7. OPTIONS Returns the HTTP methods supported by the server for a resource. Example OPTIONS /users Response Allow: GET, POST, PUT, DELETE REST API Example Method URL Action GET /users Get all users GET /users/1 Get one user POST /users Create user PUT /users/1 Replace user PATCH /users/1 Update user DELETE /users/1 Delete user Express.js Example GET app . get( "/users" , ( req , res ) => { res . json( users ); }); POST app . post( "/users" , ( req , res ) => { res . status( 201 ) . json({ message: "User Created" }); }); PUT app . put( "/users/:id" , ( req , res ) => { res . json({ message: "User Updated" }); }); PATCH app . patch( "/users/:id" , ( req , res ) => { res . json({ message: "User Partially Updated" }); }); DELETE app . delete( "/users/:id" , ( req , res ) => { res . status( 204 ) . send(); }); Safe vs Idempotent Methods Safe Methods These methods do not modify data . Method GET HEAD OPTIONS Idempotent Methods Calling these methods multiple times has the same final effect as calling them once. Method Idempotent GET ✅ Yes PUT ✅ Yes DELETE ✅ Yes HEAD ✅ Yes OPTIONS ✅ Yes PATCH ❌ Not guaranteed POST ❌ No HTTP Request Flow Client │ ▼ HTTP Method (GET/POST/PUT...) │ ▼ Server │ ▼ Database │ ▼ Response Real-Life Example 🛒 Shopping Website View Products │ ▼ GET Add Product │ ▼ POST Update Product │ ▼ PUT / PATCH Delete Product │ ▼ DELETE Interview Question Which HTTP method is used to create a new resource? Answer POST Which method updates only selected fields? Answer PATCH Which method replaces the entire resource? Answer PUT Best Practices ✅ Use GET only for reading data. ✅ Use POST for creating resources. ✅ Use PUT for full updates. ✅ Use PATCH for partial updates. ✅ Use DELETE for removing resources. ✅ Return appropriate HTTP status codes with each method. Key Points ✅ HTTP Methods define the action to perform on a resource. ✅ GET retrieves data. ✅ POST creates a new resource. ✅ PUT replaces an existing resource. ✅ PATCH updates specific fields. ✅ DELETE removes a resource. ✅ HEAD returns headers only. ✅ OPTIONS returns supported methods. 🧠 Easy Formula Need to Read? │ ▼ GET Need to Create? │ ▼ POST Need Full Update? │ ▼ PUT Need Partial Update? │ ▼ PATCH Need Delete? │ ▼ DELETE Need Headers Only? │ ▼ HEAD Need Supported Methods? │ ▼ OPTIONS GET → Read POST → Create PUT → Replace PATCH → Partial Update DELETE → Remove HEAD → Headers Only OPTIONS → Allowed Methods Remember: 📖 GET = Read data ➕ POST = Create data ✏️ PUT = Replace entire resource 📝 PATCH = Update specific fields 🗑️ DELETE = Remove resource 📋 HEAD = Response headers only ⚙️ OPTIONS = Check supported HTTP methods