Creating a custom RESTful Web Service in Drupal 9
The web is progressing and it has become quite intense for interaction between clients and websites or apps, multiple sites, databases and devices. For that the internet was demanding a new technology. One such technology which can handle this type of interaction is REST.
Before creating our own custom REST API in Drupal, we need to know what REST API actually is? It is an application programming interface (API or web API) that allows interaction with RESTful web services. Where, REST is known as REpresentational State Transfer, which was defined by Computer Scientist “Roy Fielding”.
While designing APIs, there is always a need to specify which HTTP method will be used for performing CRUD operations on data. Here, it can be made possible by using several HTTP requests like POST (Create the data), GET (Retrieve/Read the data), DELETE (Delete the data) or PATCH/PUT (Update the data). To learn how to create custom web services, we would create a custom module exposing RESTful API which can display a list of taxonomy terms from our Drupal 9 site.
Step 1: Create a custom module in the “\web\modules\custom” folder.
Step 2: Create an info.yml file as “sample_rest_resource.info.yml” along with a blank module file as "sample_rest_resource.module"
Now the folder structure will look like this:
We are now moving forward towards implementation of a rest resource using the GET method. By GET method, we need to fetch the list of taxonomy term “vb”.
Step 3: Create a folder structure in the custom module as “src\Plugin\rest\resource”.
The folder structure will look like this:
Step 4: We will implement our resource file. For that, create a php file as “SampleGetRestResource.php” and place the file in the “resource” folder.
We’re using the “GET” method in the template, in which we define our logic in the form of code to define what output is required. As an example- we require all the existing terms title with their respective IDs for “vb (machine name of Vb Test)” taxonomy for currently logged user.
Step 5: We have created a “Vb Test” taxonomy and added two terms in that.
Step 6: Now let's first enable Drupal REST API which is provided by core itself.
Our module will be dependent upon the Rest and Rest UI module. The Rest UI module can be installed using this command:
“composer require 'drupal/restui:^1.20'”
After installing the RestUI module, enable both the modules and follow the snapshots attached to configure REST API.
Step 7: Let’s enable our custom module now.
Step 8: After enabling the custom module, enable custom rest resource.
For enabling custom rest resource, follow these steps:-
1. Go to “/admin/config/services/rest”.
2. Search for our custom rest resource using the id that we have created in the resource file. Here it is “Custom Get Rest Resource”
3. Enable the custom rest resource and do the following configuration. Hit “Save Configuration”.
4. Now go to “/vb-rest?_format=json” and the URL will result in a Taxonomy term list containing Term ID and title.
So we have seen REST can be used as a type of data transfer which is built, based on the architecture of the HTTP protocols. It helps us to easily send and retrieve data between two or more different services using XML or JSON. RESTful web services are loosely coupled and are lightweight web services. These are particularly used for creating APIs for clients spread across the web.