Author : Venkateswara Rao Adapa
Drupal is one of the world’s most popular and widely used CMS frameworks.
Drupal’s back-end system is strong and can be used for managing any type of content, including text, geospatial, categories and media.
What Is Headless Drupal?
The term “Headless Drupal” refers to using Drupal as backend system and Rest server, so that content operations (e.g., create, read, update, delete) can be done from another language or tool. Drupal 8 comes with Rest Server included in its core. Drupal 7 uses a contributed module called Services or RestWS to achieve the same functionality.
In today’s world, the diversity of computing tools is wider than ever. People are using a combination of desktops, laptops, tablets and smartphones to explore content on whatever device they choose. Likewise, businesses are aggressively targeting smartphone users and seek to provide services and gain sales through mobile apps, offers and ads.
Thus, front-end frameworks were invented to provide a better user experience while viewing content on a wide variety of devices.
About REST (Representational State Transfer) API
RESTful API has gained more popularity than SOAP- or WSDL-based web services because of faster development and easier usage. RESTful web services are resource-based and can work with a variety of “resources” including users, articles and categories. Although there are no strict rules for RESTful API creation, there are some good principles to follow, including how to change resource states and transfer them over HTTP.
There are four basic HTTP methods available to Create, Read, Update and Delete resources on a server.
GET: Retrieves a resource from a server
POST: Creates a resource in a server
PUT: Updates a resource on a server
DELETE: Deletes a resource from a server
A good developer should always follow these principles correctly to maintain consistency. See the example below.
Poor Practice: GET /api/addArticle?name=Foo HTTP/1.1
This is not a good design because the above web method “addArticle” supports a state-changing operation over HTTP GET, and upon successful execution, a new article with name Foo will be added to the database. Web servers are designed to respond to HTTP GET requests by retrieving resources that match the path (or the query criteria) in the URI request and return them in a response—not add a record to a database. The GET method should be used when retrieving articles like: GET /api/getArticles HTTP/1.1.
Good Practice: POST /api/addArticle?name=Foo HTTP/1.1
This is a better practice for adding a new resource (article) to the database, instructing the client to use the correct method (POST) to send the request.
The response from server can be XML or JSON.
Most people prefer JSON as the response type since it is lightweight and can be parsed easily by any client side language.
The Drupal Services module can expose the resource nodes, users and taxonomies in RESTful way. In addition, with custom code we can deliver a JSON response instead of HTML by setting “delivery callback”; as “drupal_json_output” in the hook_menu implementation.
How Front-End Frameworks Interacts with the REST Server
Using Headless Drupal, content-rich back-end work can be done very quickly and data can be exposed through REST API. Front-end work can be done by using any of the desired front-end frameworks (e.g., Angular JS, Backbone and others). We can also use the same REST API for developing mobile applications.
Through this, we can build different kinds of applications, including single page applications (SPA), responsive apps and mobile apps, with a single Drupal back-end system.