HTTP Methods


HTTP methods (also known as verbs) define the actions to be performed on a resource. They are the foundation of how clients interact with servers over the web. Understanding these methods is crucial for building any web application.

Here are some of the most common HTTP methods:

Example: JavaScript Fetch API

Here’s how you might use these methods with JavaScript’s fetch API:

// GET request
fetch('/users')
  .then(response => response.json())
  .then(data => console.log(data));

// POST request
fetch('/users', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ name: 'John Doe', email: 'john.doe@example.com' }),
})
  .then(response => response.json())
  .then(data => console.log(data));


// PUT request
fetch('/users/123', {
  method: 'PUT',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ name: 'Jane Doe', email: 'jane.doe@example.com' }),
})
  .then(response => response.json())
  .then(data => console.log(data));

// DELETE Request
fetch('/users/123', { method: 'DELETE' })
  .then(response => console.log(response.status)); // Check the response status

Understanding these fundamental HTTP methods is key to effectively interacting with web services and building robust web applications. Each method serves a specific purpose, contributing to the clear and structured exchange of information between clients and servers.