Getting Started with Drupal 8 REST

I am getting back into Drupal web design, since it is incorporating into its core codebase RESTful web services as well as responsive themes. That makes it a powerful, versatile CMS that can be adapted to apps and various form factors. I once populated a Drupal 7 web site with the Feeds Module, importing a CSV file into a custom Content Type. I then unlocked a lot of the information of those nodes using the RESTful Web Services module. There wasn’t a useful module to replicate this setup for Drupal 8, so here’s my workaround using the integrated REST capabilities.

  1. Installed Composer


  2. Composer is the dependency manager for PHP, similar to npm for node.js.

  3. Installed Drush 8.x(dev)


  4. Using Composer makes downloading and installing drush, Drupal’s command-line utility, a one-line affair.

  5. Wrote a makefile

  6. Drush can reference a makefile to install and configure a Drupal installation from the command line. In my web server’s directory, I wrote this make.yml:

    The output looks like this

    web@linux /var/www/html $ drush make make.yml ./drupal8
    Beginning to build make.yml. [ok]
    drupal-8.0.0-beta15 downloaded. [ok]
    restui cloned from http://git.drupal.org/project/restui.git. [ok]
    Checked out branch 8.x-1.x. [ok]

  7. Drush Site Install

  8. The drush site-install command uses the makefile in the present directory along with the arguments to the SQL database you need. I am also defining the administrator username and password.

  9. Drush Module Enable

  10. One line turns on all of the prerequisite modules.

  11. Content Type created, RESTpermissions set

  12. Create your Content Type. In my example, it has a title and two text fields. The title is the name of a fruit or vegetable. One text field describes how you can identify that the produce is ripe, and the second field describes how you can store it appropriately to maintain its freshness and prevent it from spoiling prematurely. Under the Drupal Administration Menu, navigate to People->Permissions. Scroll down to the section titled Node. Allow Authenticated users to create new content for the content type you just created. Then scroll down to RESTful Web Services and enable Authenticated Users to GET, POST, PATCH, and DELETE (see image below). Finally, Drupal protects your site from cross-site scripting exploits, so make sure your trusted_host_settings are defined in settings.php of the Drupal installation.

    Creating Proper Permissions

  13. Python Script

  14. With a little Python, plus the Requests library, we can put the API we created to the test.

The results look something like this:
Drupal8Item

2 thoughts on “Getting Started with Drupal 8 REST

  1. Very nice article. I wish you elaborate more about using pathon script. One thing I couldn’t understand : why did you create make file for restui. Why didnt you use Drush dl restui (as there is already a module). IN any case, this seems to be a good way to import content through csv files, which feeds module couldn’t achieve yet in Drupal 8. In any case it sounds great. Did you try fetching n mapping image and other fields ( such as taxonomy) this way? Will it work for them too? Please provide some more hint so that one could use it in production for importing bulk content. Thanks & Regards

  2. You make a good point that you can use “drush dl” to download the RestUI module in a Drupal 8 site that has already been installed. The makefile is read by drush during the “drush site-install” command, so it fetches the RestUI module during the initial installation. It may be overkill since that is the only module I am using in this example, but if you are maintaining multiple sites with similar module loads a makefile can save you effort in the long run.

    The Python script can be a little tricky to decipher. In the first few lines I am loading the necessary Python libraries and navigating to the directory which has the CSV file I am trying to import. Line 9 defines the url where I am sending my request, defined using the RestUI Drupal module. Lines 11-13 builds a variable “s”, where I am setting the username and password as well as the HTTP headers using the Requests library. Finally, I am iterating through the CSV file, defining dictionaries, or key-value pairs for Drupal to translate into content nodes in its site.

    You can also edit and create taxonomy terms and vocabulary, as well as image style fields, using RESTful services. I have not used those, but I imagine if you construct the right format for your CSV you can save a lot of effort creating a taxonomy. Thank you for your feedback, Manu.

Leave a Reply

Your email address will not be published. Required fields are marked *