This tutorial aims to help you understand the different HTTP methods used in REST APIs.
By the end of this tutorial, you will be able to:
- Understand what HTTP methods are and how they function in REST APIs.
- Differentiate between GET, POST, PUT, DELETE, PATCH, and OPTIONS methods.
- Implement these methods in your own REST APIs.
Basic knowledge of web development, HTTP, and APIs is recommended but not necessarily required.
HTTP methods define the type of action a request intends to perform. They are sometimes referred to as HTTP verbs. The following are the most commonly used HTTP methods in REST APIs:
This method retrieves information from the specified source. It doesn't change the state of the resource and is therefore considered safe. It should only retrieve data and should have no other effect.
This method sends data to the server to create a new resource. The data is included in the body of the request. This may result in the creation of a new resource or the updates of existing resources.
This method is used to update a current resource with new data. The request will contain the updated data. The PUT method is idempotent. That means calling the same PUT request multiple times will always produce the same result.
This method deletes the specified resource.
This method is used to apply partial modifications to a resource.
This method returns the HTTP methods that the server supports for the specified URL.
Let's look at some code snippets to understand these methods better.
app.get('/users', function(req, res) {
// Code to fetch users
res.send('GET request to the users page');
});
In the above code, we define a GET request to fetch users. When the '/users' endpoint is hit with a GET request, the server responds by sending 'GET request to the users page'.
app.post('/users', function(req, res) {
// Code to create a new user
res.send('POST request to the users page');
});
Here, we define a POST request to create a new user. The server responds by sending 'POST request to the users page'.
app.put('/users/:id', function(req, res) {
// Code to update a user
res.send(`PUT request to the user ${req.params.id}`);
});
This example shows a PUT request to update a user. The server responds by sending 'PUT request to the user {id}'.
app.delete('/users/:id', function(req, res) {
// Code to delete a user
res.send(`DELETE request to delete user ${req.params.id}`);
});
This DELETE request deletes a user. The server responds by sending 'DELETE request to delete user {id}'.
app.patch('/users/:id', function(req, res) {
// Code to partially update a user
res.send(`PATCH request to update user ${req.params.id}`);
});
This PATCH request partially updates a user. The server responds by sending 'PATCH request to update user {id}'.
app.options('/users', function(req, res) {
// Code to send the HTTP methods that the server supports for the specified URL
res.send('OPTIONS request to the users page');
});
The OPTIONS request returns the HTTP methods that the server supports for the specified URL. The server responds by sending 'OPTIONS request to the users page'.
In this tutorial, we discussed the different HTTP methods used in REST APIs, including GET, POST, PUT, DELETE, PATCH, and OPTIONS. Each of these methods has a specific purpose and is important to understand when developing APIs.
These exercises will give you practical experience and reinforce what you've learned. Remember, the best way to learn is by doing. Happy coding!