← Back to Chapters

HTTP Methods (GET, POST, PUT, PATCH, DELETE)

? HTTP Methods (GET, POST, PUT, PATCH, DELETE)

? Quick Overview

HTTP methods define how a client communicates with a server. They describe the type of action to perform on a resource such as fetching data, creating records, updating content, or deleting data. These methods are heavily used in REST APIs and tools like Postman.

? Key Concepts

  • GET Retrieve data from server
  • POST Send new data to server
  • PUT Replace existing data
  • PATCH Partially update data
  • DELETE Remove data

? Syntax / Theory

Each HTTP request consists of a method, URL, headers, and optionally a body. The server responds with a status code such as 200 (OK), 201 (Created), 404 (Not Found), or 500 (Server Error).

? Code Examples

? View Code Example
// GET request example using fetch API
fetch("https://api.example.com/users")
.then(response => response.json())
.then(data => console.log(data));
? View Code Example
// POST request to send new data to server
fetch("https://api.example.com/users", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Meghraj", role: "Developer" })
});
? View Code Example
// PUT request replaces the entire resource
fetch("https://api.example.com/users/1", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Updated Name", role: "Admin" })
});
? View Code Example
// PATCH request updates specific fields
fetch("https://api.example.com/users/1", {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ role: "Manager" })
});
? View Code Example
// DELETE request removes a resource
fetch("https://api.example.com/users/1", {
method: "DELETE"
});

? Live Output / Explanation

What Happens?

  • GET → Server returns requested data
  • POST → New record created
  • PUT → Entire record replaced
  • PATCH → Specific fields updated
  • DELETE → Record removed

? Interactive Example

Click the buttons below to simulate a live API request and see the response.

// Console awaiting input... Click a button above to start.

? Use Cases

  • GET – Fetch product lists, user profiles
  • POST – User registration, form submission
  • PUT – Update user settings
  • PATCH – Change password or status
  • DELETE – Remove accounts or records

✅ Tips & Best Practices

  • Use GET only for reading data
  • Never send sensitive data in GET URLs
  • Prefer PATCH for partial updates
  • Always handle HTTP status codes

? Try It Yourself

  • Create a GET request in Postman
  • Send a POST request with JSON body
  • Modify data using PUT and PATCH
  • Delete a record using DELETE