DEV Community

Cover image for HTTP Methods In A Nutshell
Sharif
Sharif

Posted on

HTTP Methods In A Nutshell

The HyperText Transfer Protocol is designed to enable communications between clients and servers. The protocol works by clients sending requests to the servers and servers responding to the requests. We do CRUD operations by sending HTTP requests with different HTTP methods. Sometimes it is also called HTTP verbs.

Types of HTTP Methods:
GET
POST
PUT
PATCH
DELETE
HEAD
OPTIONS
TRACE
CONNECT

GET Method
If we want to retrieve data from a resource like websites, servers or APIs, we send them a GET Request. For example, we send a GET request to the server if we want a list of our users or a specific user from the database.

Example:
Image description Image description

POST Method
The POST method creates a new resource on the backend. The request body carries the data we want to the server. It is neither a safe nor idempotent method. We don't expect to get the same result every time we send a POST request.

Example:
Image description Image description

PUT Method
With the PUT request method, we can update an existing resource by sending the updated data as the content of the request body to the server. The PUT method updates a resource by replacing its entire content completely.

Example:
Image description

PATCH Method
PATCH is another HTTP method which is similar to PUT. But it is used to update data partially and not entirely. Such as, a product has some properties like name, price, type, brand, etc. We want to update the value of product price by PATCH method not the whole product properties.

Example:

body JSON: {
β€œprice” : 299,
}
Enter fullscreen mode Exit fullscreen mode

Image description

DELETE Method
The DELETE method deletes resources. The DELETE method is idempotent; regardless of the number of calls, it returns the same result.

Example:
Image description

HEAD Method
The HEAD method is similar to the GET method. But it does not have any response body, so if it mistakenly returns the response body, it must be ignored. One of the advantages of the HEAD method is that we can test the server if it is available and accessible as long as the API supports it, and it is much faster than the GET method because it has no response body.

OPTIONS Method
Options method is used to get information about the possible communication options (Permitted HTTP methods) for the given URL in the server to refer to the entire server. This method is safe and idempotent. Modern browsers widely use the OPTIONS method to check whether the CORS (Cross-Origin Resource Sharing) operation is restricted on the targeted API or not.

TRACE Method
The TRACE method is used to perform a message loop-back test that tests the path for the target resource. It is useful for debugging purposes. The TRACE method could be dangerous because it could reveal credentials. A hacker could steal credentials, including internal authentication headers, using a client-side attack.

CONNECT Method
The Connect method is for making end-to-end connections between a client and a server. It makes a two-way connection like a tunnel between them. For example, we can use this method to safely transfer a large file between the client and the server.


Read more on HTTP:
https://www.geeksforgeeks.org/different-kinds-of-http-requests
https://testfully.io/blog/http-methods
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol

Top comments (0)