HTTP request methods are used to request an action that you want the server to perform on the given URL address. Let us understand this with the help of a real-life situation:
An HTTP request method has a message head and a message body. The head consists of the URL and the headers, whereas the body consists of the information to be sent.
These are the following HTTP request methods:
Let’s understand these methods in some detail.
GET
The GET method is used to request a particular resource from a server. The response could be any data ranging from a CSS file to a JSON object. Following are some features of GET method:
Following are some examples of a GET request:
For example, when you enter the name of a country in a form, the list of cities in that country appears automatically. In this case, when you choose a country, a GET request is sent to the server, asking it to send you a list of the cities in that country.
POST
As the name suggests, POST requests are used to post data to a given URL. They can be used to make the server store some entries such as the data you fill in the signup form on a website or the open text answers you write on our platform; these are all sent to the server in a POST request. Following are some features of POST method:
At this point, it is important to understand the difference between GET and POST.
PUT
The PUT request is a special request which performs a function similar to a POST call. But this raises the question — why do you need a PUT call? The reasons and use cases for PUT are as follows:
PATCH
The PATCH request can be understood as something similar to the ‘update’ function. While PUT request completely modifies the entire resource, PATCH request only modifies the part of the resource that needs to be updated. Also, PATCH calls are not necessarily idempotent.
To understand the difference between PUT and PATCH, let’s consider the following image:
Suppose a person is working in the company ‘Appzroot’ and the location of work is ‘Bengaluru’.
Now, his/her location of work has to be changed to ‘Mumbai’.
If you use the PUT method to update the details, you will have to send the complete resource, i.e., {“company”: “Appzroot”, “location”: “Mumbai”} since the PUT request will overwrite the complete resource.
However, if you use the PATCH method, you will only need to send the resource that needs to be updated, i.e., { “location”: “Mumbai”}.
This is because the PATCH request will only update what has been passed in the request.
DELETE
A DELETE request does exactly what its name suggests — it asks the server to delete some data from the given URL. A DELETE request, much like a GET request, does not contain any message body.
A DELETE request is quite similar to the delete operation one performs on a database.