Enabling web services in Drupal 8

Rest Drupal 8 Logo

Drupal 8 puts a lot of effort on creating decoupled architectures, this kind of architectures follow the paradigm: "Create once, publish everywhere", this means they are meant to allow us to create content once in our content manager and allow us to use it on every device/format/client without needing to create content for each channel.

But... What is REST?

In order to make sure we are all in sync: Web services allow other apps to interact with our content server in a manner more efficient than a plain web browser, this kind of interactions use to allow CRUD operations (Create, Read, Update, Delete)

REST is one of the most web service formats, there are other formats like SOAP or XML-RPC, we will focus only in REST since it's the standard format in Drupal (although other two are available as modules) REST uses the HTTP methods (GET, POST, PATCH, PUT, DELETE).

This kind of services are used to show content in other portals or to interact with other apps, an example can be mobile applications.

RESTful in Drupal.

Drupal ships in core distribution all needed modules to publish content in REST format:

RESTful Web Services (rest)

Exposes drupal entities and other resources (like views) though a RESTful web API. Uses  Serialization module to serialize content sent through API.

Serialization

As seaid above it's the module used to serialize, that's coding data so it can be more efficiently sent, includes most used formats: JSON and XML.

Hypertext Application Language (hal)

Improves serialization module offering HAL format. This format adds two new reserved words: '_links' for relationships and '_embedded' for embedded resources. Can be offered both in JSON and XML format.

HTTP Basic Authentication (basic_auth)

This module is used to provide authentication mechanism for Basic HTTP, there are modules providing more secure modules, in further examples we will create services available for anonymous users so we will avoid this by now.

REST UI

This module is not incluided in core, but it's needed to configure services in administration module since provides interface to manage REST services.

Starting over:

First we need to activate modules, this can be done with drush or admin UI:

Modulos activados para RESTful

Once modules are activated we can activate services in the admin section (admin/config):

Apartado de configuracion

Once inside REST, we can choose which entities and elements of Drupal we want to expose as web services and control the way it is done, in this example we will expose content in JSON and HAL_JSON format, only for GET method and we will handle authentication with Cookies:

Configuración de contenido REST

Once it's done, we have an additional endpoint for our content, now we can request the content in html format as usual with browser or we can request content in one of the available REST formats:

https://praderas.org/node/1?_format=json

Respuesta a peticion REST

In the same way we can publish content using REST we can publish view results, once services module is activated we will have a new Presentation available for views, so in the same way we can create blocks or pages now we will be able to create REST exports from our views.

We will create a view that will allow us to list all web content in REST format, we will further use it to create a small mobile web version, there is no special instructions to create the view, besides choosing REST export as presentation.

In REST exports we can expose all entity or just a serie of fields at our discretion, in this case we will only need to export a serie of fields, not the whole entity.

While developing, it's highly recommended to disable the view's cache because if there is a wrong result while manipulating view it can be cached and we can lose a lot of time trying to guess why it's still failing.

So an example view would be as follow:

Vista de exportación REST

We can use any REST tool to check the answer of our endpoint:

Respuesta de exportacion REST

With this, we have exposed our nodes with a REST service and we can use JSON and HAL_JSON formats. We also have a view that will answer with a REST export of all content in web.

In our next article we will use jquery mobile to create a mobile version that will use the data from our REST exportation.

Next article(Spanish version):

Pequeña Versión Móvil con Jquery Mobile.

(this content was originally created in Spanish in 2018, some content might be outdated this is just part of the translation effort on the site)

Tags