REST mode in Drupal 8

Submitted by nmeegama on Mon, 06/20/2016 - 10:32

As you might already know Drupal 8 contains a web services layer out of the box. Therefore we can now easily create a RESTful Architecture by exposing Drupal's data to a third party application. REST stands for Representational State Transfer. It is a set of design principles for making network communication more scalable and flexible. Drupal 8 Core strictly adopt REST principles thus allowing the ability to convert/serialize content into JSON or XML formats which are common formats used and understood by most of the web services implemented. Therefore it will be very straight forward for developers if they ever decide to expose Drupal as a web service to other web applications. So now lets look at how we can configure Drupal 8 to use it as a SERVER in a RESTful Architecture

Core Modules for REST

  • RESTful Web Services : A module to expose Drupal entities as resources by converting Drupal into a REST server
  • Serialization : A module to help serialize and de-serialize data to and from formats such as JSON and XML
  • Basic Auth: A module to provide basic authentication for communicating with third party applications
  • HAL: Serializes content in HAL(Hypertext Application Language).

We need to enable RESTful Web ServicesBasic Auth and  Serialization

Configuration

We really don't need to configure anything for the modules. But we need to tell Drupal how the REST endpoints should respond and what to expect. The way we say this is by adding YAML file with the relevant configuration parameters. The name of the file rest.settings.yml. There is a sample file here core/modules/rest/config/install/rest.settings.yml. We need to copy the contents of this file change it as to how we need it and upload it here /admin/config/development/configuration/single/import. Sample configuration:

resources:
  entity:node:
    GET:
      supported_formats:
        - json
      supported_auth:
        - basic_auth
    POST:
      supported_formats:
        - json
      supported_auth:
        - basic_auth
    PATCH:
      supported_formats:
        - json
      supported_auth:
        - basic_auth
    DELETE:
      supported_formats:
        - json
      supported_auth:
        - basic_auth
link_domain: ~

Testing

Now we can test using a plugin like postman. This tool has a lot of functionalities when it come to testing RESTful API's. For example it will let you addd headers and let you save your requests for future reference ...etc:-. Below is a screenshot of the tool

postman

Making things easy with contributed modules

If you thought this was a bit hard and you need a user interface to tell Drupal what to do then, I say OK lets install a contributed module. The name of the module is REST UI. You can download and install the module here. Else use drush or drupal console. I like Drupal Console because it also clears the cache for me 

 

dc install restui

Once the module is installed GOTO /admin/config/services/rest and configure. Since we have already manually added the res.settings.yml file content through the configuration import interface the Content resource will already be configured as per screen shot below.

rest ui config 1

 We can enable and edit any configurationon this screen. Also if you need HAL format then just enable the hal module as well.

 

Unlocking the full potential with contributed modules

Although the core rest module supports most common use cases it might not be the ideal solution for a more sophisticated architecture. In case you really need to tap into the inner force of Drupal 8 then you should really try using some of the contributed modules. One such module is the RELAXed module which enable the following extended features.

  • Translation support
  • Revisions support
  • UUID's sharing
  • File attachments

Else the services module for Drupal 8 also support an array of extended features like

  • Custom endpoints configuration
  • UUID sharing
  • Sharing Drupal entities like Blocks or Menus

Until certain features are developed with core's rest module we can use the above alternatives to build great solutions.

Conclusion

There is a huge adoption of javascript frameworks and with such a trend coming in Drupal 8 has made a correct move to include RESTful web services module in core. So as a start it is always useful to know how to use it. Hope everything makes sense and Hope you take something away. 

References

  • https://codewords.recurse.com/issues/five/what-restful-actually-means
  • http://buytaert.net/an-overview-of-web-service-solutions-in-drupal-8
  • https://www.drupal.org/developing/api/8/rest