Understanding HTTP Methods in REST APIs

Tutorial 1 of 5

Understanding HTTP Methods in REST APIs

1. Introduction

Goal

This tutorial aims to help you understand the different HTTP methods used in REST APIs.

Learning Outcomes

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.

Prerequisites

Basic knowledge of web development, HTTP, and APIs is recommended but not necessarily required.

2. Step-by-Step Guide

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:

GET

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.

POST

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.

PUT

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.

DELETE

This method deletes the specified resource.

PATCH

This method is used to apply partial modifications to a resource.

OPTIONS

This method returns the HTTP methods that the server supports for the specified URL.

3. Code Examples

Let's look at some code snippets to understand these methods better.

GET Method

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'.

POST Method

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'.

PUT Method

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}'.

DELETE Method

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}'.

PATCH Method

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}'.

OPTIONS Method

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'.

4. Summary

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.

5. Practice Exercises

  • Exercise 1: Create a REST API using the GET and POST methods to create and retrieve resources.
  • Exercise 2: Extend the API from exercise 1 to include PUT and DELETE methods to update and delete resources.
  • Exercise 3: Add options to handle OPTIONS and PATCH requests to the API from exercise 2.

These exercises will give you practical experience and reinforce what you've learned. Remember, the best way to learn is by doing. Happy coding!