HTTP Methods: A Comprehensive Guide

Eric Okemwa
2 min readApr 27, 2023

--

Image by Numpyninja

HTTP methods are the verbs used to specify the action a client wants to perform on a server resource. There are a number of different HTTP methods, each of which has a specific purpose.

The most common HTTP methods are:

Photo by Edureka
  • GET — This method is used to retrieve a representation of a resource. For example, a GET request could be used to retrieve the HTML code for a web page.
  • POST — This method is used to create a new resource or to update an existing resource. For example, a POST request could be used to create a new user account or to update a user’s profile information.
  • PUT — This method replaces an existing resource with a new one. For example, a PUT request could replace an old image with a new one.
  • DELETE — This method is used to delete a resource. For example, a DELETE request could delete a user account.
Table by Bendakhila

In addition to these common methods, there are a number of other HTTP methods that are less commonly used. These methods include:

  • HEAD — This method is similar to GET and only retrieves the response's headers. The body of the response is not returned.
  • OPTIONS — This method is used to determine the capabilities of a server resource.
  • PATCH — This method is used to update an existing resource partially.
  • CONNECT — This method establishes a tunnel to a remote server.
  • TRACE — This method is used to echo back the request that was sent to the server.

Code Examples

Here are some code examples that show how to use the different HTTP methods:

// GET request

const request = new Request('https://example.com/');

request.method = 'GET';

const response = await fetch(request);

// POST request

const request = new Request('https://example.com/users', {
method: 'POST',
body: JSON.stringify({
name: 'John Doe',
email: 'johndoe@example.com',
}),
});

const response = await fetch(request);

// PUT request

const request = new Request('https://example.com/users/123', {
method: 'PUT',
body: JSON.stringify({
name: 'Jane Doe',
email: 'janedoe@example.com',
}),
});

const response = await fetch(request);

// DELETE request

const request = new Request('https://example.com/users/123', {
method: 'DELETE',
});

const response = await fetch(request);

Conclusion

HTTP methods are an important part of HTTP requests. By understanding the different HTTP methods, you can use them to perform the desired actions on server resources.

--

--