As we know, Drupal is a popular CMS framework in industry. It is not only used for creating and manipulating digital content for websites, but it can also be used for creating web services to provide content to other applications, irrespective of language, platform via JSON, Hypertext Application Language + JSON (hal+json), xml formats, etc. Drupal contains default core modules to implement restful web service. One more contribute module REST UI is also required to manage Rest resources in Drupal, with the help of these modules and its features one can create a secure, portable, fast and flexible web service.
What is Restful Web service?
Restful web services are built to work best on the Web. Representational State Transfer (REST) is an architectural style that specifies constraints, such as the uniform interface, that if applied to a web service induce desirable properties, such as performance, scalability, and modifiability that enable services to work best on the Web.
In the REST architectural style, data and functionality are considered resources and are accessed using Uniform Resource Identifiers (URIs), typically links on the Web. The resources are acted upon by using a set of simple, well-defined operations. The REST architectural style constrains an architecture to a client/server architecture and is designed to use a stateless communication protocol, typically HTTP. In the REST architecture style, clients and servers exchange representations of resources by using a standardized interface and protocol.
Advantages of REST:
- Simple and easy to implement
- You can make good use of HTTP cache and proxy server to help you handle high load
- It helps you organize even a very complex application into simple resources.
- By using HTTP methods (GET, POST, PATCH, DELETE, etc.) can interact and be controlled over the other third party application
- It makes it easy for new clients to use your application, even if you haven’t designed it specifically for them (probably, because they weren’t around when you created your app)
RESTful Web Services in Drupal 8
Rest architecture in Drupal 8 is as shown below. REST is one of the most popular ways of making Web Services work. There are other formats such as SOAP or XML-RPC, but we are only going to focus on REST because it is the Drupal standard. In Drupal REST utilizes HTTP methods, such as GET, POST, and DELETE.
REST Architecture in Drupal 8
So far, we explained about REST and its architecture in Drupal 8. Next, we would show how we have utilized this tool to create and handle Rest APIs in Drupal 8 hosted on IIS Server in the project.
- Project requirement
- Implementation and configuration of Drupal 8 rest service into the project
- Testing web services by using Advanced Rest Client (ARC) tool
- Problems and challenges faced during development, deployment, and solution toward problem solving
- Advantages of Drupal rest service over other technologies web services
Project requirement: Skill Management System
Skills management is the portal to update the skills of all the employees in an organization. Skill manager (admin) is responsible to update the skills of all the employees from the portal. The front end of the portal was developed using HTML5 and ReactJS and we needed to create an API to perform all the required CRUD operation for the portal. To achieve this, we have provided a REST API created using Drupal 8 open source technology.
Implementation and configuration of Drupal 8 rest service into that project
REST Web Services have been implemented in Drupal 8 Core with the following modules:
- RESTful Web Services (REST)
- Hypertext Application Language
- HTTP Basic Authentication (basic_auth)
The above list of modules need to be enabled at Drupal extension before starting the Rest API. See the below screenshot for reference:
A handy contributed module REST UI needs to be downloaded for resource handling. This module provides a user interface for enabling and disabling resources, serialization formats, and authentication providers.
See the screenshot below for an example of the configuration options REST UI provides.
Let us take this opportunity to explain the available resource options, GET, POST, PATCH, and DELETE. These are all common HTTP methods. Each operation signifies a type of action that needs to be performed on a resource. For example, your browser used GET when you requested to view the blog post. To read a resource, we use GET. To create a resource, we use POST. To update a resource, we use PATCH. Lastly, to delete a resource, we use DELETE.
Here we have used content and taxonomy REST resources to achieve the requirement. Content resources are enabled for getting employee details. Taxonomy resources are enabled for skills, which are used to categorising the employee details based on his/her related skills.
By using these two resources, skills can be tagged to a particular employee, and can also manage and track all employees based on their skills.
Below are the URL methods for reference, which we handled, for CRUD operations:
Testing web API by using Advanced Rest Client (ARC) tool
Implemented REST API can be tested with tools like:
- Advanced Rest Client
- Postman-Rest Client
- Curl in Linux
Here we will be using Advanced Rest Client, to get Advanced Rest Client.
- Go to Google Chrome’s Web Store and Search for “Advanced Rest Client” and Install the extension
- Select the “Advanced Rest Client” icon under chrome’s app section – chrome://apps/
- Once the installation is done, enter the URL of the API in the URL textbox and select the type of the HTTP method, HEADER and few options if required.
Then hit the “send” button. After clicking on “send” button, we will get a response with results and it will populate an error message if header and body input values are improper.
Advantages of Drupal 8 rest service over other technologies web services:
- Drupal 8 Open source content management framework (https://www.drupal.org/download).
- Drupal core web service modules to REST API implementation makes simple
- Contributed module RESTUI also available at drupal.org site, it useful for handling rest resource of different entities
- Compared to other technologies like ASP.Net, JAVA, PHP MVC framework, using Drupal Rest API are developed Quick and less code
- Configuration and installation of modules are enough to create and handle secured REST APIs
- Authentication can be implement to the REST by enabling basic_auth, cookie options at REST UI
Problems and challenges faced during development and deployment, and the solutions toward problem solving
1. No CSS loading on fresh install of Drupal 8.0
2. Delete HTTP method was not allowed on IIS server
Solution: Add below code in web.config file
3. Cross-origin resource sharing (CROS) Error
Solution: Add below code in web.config file
Drupal is one of the most reliable, efficient and robust content management systems. Rest architecture in Drupal 8 are easy to understand, implement and manage REST APIs for third party application.
HTTP standard makes REST a very attractive option for public APIs. Coupled together with JSON, which makes something like adding an optional parameter is very simple, makes it very flexible and allows for frequent releases without any impact.
REST a style of architecture for hypermedia apps that are networked. Due to REST integration, any content from Drupal can be made available via the REST API. This can be done by enabling the web services module with Drupal’s core configurations.
Problems and challenges faced during the development are the configuration issues with IIS server as mentioned above. The solution is provided to overcome these issues, which should be helpful in implementing Drupal 8 rest service with IIS server. Without writing much code, user can develop REST API in Drupal with proper setup and configuration. Drupal is a stable CMS framework available with web services modules (core and contributed) and user-friendly interfaces, which makes it simple for user to develop REST API.
Latest posts by Vinod & Shashikanth
- Designing a RESTful Web Service using Drupal Hosted in IIS Server - February 27, 2019