In this quick tutorial, we will expose our Drupal 8 site content through a RESTful API. To achieve this, we will use Drupal 8 RESTful Web Services and the Serialization Module. These modules are included in Drupal 8 core, so go ahead and grab a copy of latest Drupal 8 version (at the time of this tutorial the latest Drupal 8 verison was drupal-8-aplha-13). Without further ado, let's start!

Step 1 - Enable the two modules. Under Web Services, enable Restful Web Services and Serialization Module.

Enabling Modules

Step 2 - Configure the permissions to allow users of your API to make GET requests.

Configuring Permissions

 Step 3 - Final step. We need to create a view to serve the API.

  • Create a new view with REST Export display
  • Select the Format as Serializer, and, in the setting, check the JSON/XML option, or both depending on your needs
  • Configure the filter criteria depending on your needs; I just choose the option to filter by article content type
  • Put the path which you want people to make requests to get the data. I named my path as ‘all-content’
  • By default this view exposes the whole entity data but if you want more control you can select the fields that you want to show. For e.g you can show just the title of all nodes and nothing else

Setting Up View

BOOM!!! we are done. Now you can point the browser to www.your-site.com/all-content and see the sweet json. I am using DHC extension for chrome to make the GET request. Here is a simple get request for example,

Final Result

BONUS!!! 

We can also do POST requests using the same method. For doing POST requests we need to enable HTTP Basic Authentication. Again, HTTP Basic Authentication module is in Drupal 8 core, so we just have to enable it. Also, You have to update your permissons for POST in RESTful Web Services section. Here is the final result:

Drupal 8 Post Request

Read Next

Hacking JSON (de)serialization in Mule

04 August, 2014|3 min