Vasilis Papoutsakis's picture

Working with WebServices in Drupal 8

Connect with the machine-readable web
24.10.13

Let’s say, we have a shop with a Drupal website showing its products’ detailed info. And we want to make a mobile application which can easily interact with drupal with full CRUD functionality. For example, we want customers be able to view products’ details and search for products and to be possible for the shop’s staff to update the details of a product.

What we need is to extend Drupal to receive and send data in machine readable format so it would be easier for the app developer to make the app. So we need to make Drupal support a webservice!

How will it work? In the website, we have a content type “products” with details about the products. We need the app to use our website using simple requests, which can be made using RESTful Web API. REST uses the HTTP methods for communication between 2 devices in machine-readable format (for example JSON or XML) and is widely used in the WWW. It takes advantage of HTTP Header’s Media Type (MIME) to select export data format and the action the HTTP method used determines the action performed.

Comparison with Drupal 7

Have you ever tried to implement a WebService on Drupal 7?

Drupal Community have made progress in that area. There are a few ways to do it, for example by using RESTful Web Services (restws) or Services modules, maybe some extras and/or write some custom code.

But both modules have their limitations. Services endpoint URIs are custom URL paths, it uses drupal_form_submit() for write operations, covers only core entities (there is an extra module to support Entity API) and doesn’t use hypermedia header. RESTful Web Services on the other hand, can support Entity API has no endpoints and doesn’t use hypermedia (eg. node/1.json) and has no access controls based on entity or field API.

Now.. Drupal 8 with Symfony2 takes it a bit further. REST is now in Drupal 8 Core.

Supports all New Entity API and fields and has access control on both entity & field level.

Operations are supported on the resource itself. For example:

Action Method
Create POST /entity/node
Read GET /entity/node/1
Update PATCH /entity/node/1
Delete DELETE /entity/node/1

(Might change to “/node/1” on later releases.)

How it’s done

Imagine the simpliest way for the site builder to implement them. Now let’s see if it's simpler than Drupal 8.

Step 1: Enable the Modules from Drupal 8 Core:

(HTTP Basic Authentication is not working properly yet: https://drupal.org/node/2099439)

Step 2: Set permisions if needed:

For example we enabled anonymous users to access GET requests for nodes.

or by using drush:

$ drush role-add-perm anonymous 'restful get entity:node'

Then… Done!

Every entity can be accessed with REST requests.

Do you want to export a Views result? Just add REST export to views display:

Performance Point of View

Drupal 8 is not final yet, and there are some major changes in its core (compared to Drupal 7). There are many improvements to be done including fixing some performance regressions https://drupal.org/node/1744302 . A straight comparison with the more mature Drupal 7 wouldn’t be “fair”.

But using Symfony2’s components and architecture in Drupal 8 there are some pros on its possibilities to improve performance. There is no longer need to initialize the whole bootstrap, thus will use less memory, plus its caching architecture is better than Drupal 7’s allowing improvements in several layers.

 

[Image by: John Maravelakis]