Drupal 8 brought along with it many notable features which have made it easier to use and develop for the platform. One such feature was the incorporation of RESTful web services in Drupal 8 core for API calls. Using RESTful web services, a host of possibilities for customization of the platform open up; not to mention that these web services are the underlying principles which enable the concept of ‘headless Drupal’. In this post, I’ll start by performing a very basic Drupal function using these web services, i.e. creating a node.
Start by enabling the following 4 core modules in Drupal:
- HTTP Basic Authentication;
- RESTful Web Services;
Download the REST UI module as well, since it allows changing permissions and settings through a simple GUI, negating the need to go into the rest.settings.yml file in order to do the same changes.
Create User and Set Permissions
I’m now going to create a new authenticated user for the site. I’ll be doing this to teach you the kind of permissions that need to be set. Note that if you log in as an admin, all the following permissions will already be enabled.
Now, create a new user and navigate to admin/configuration/web services/REST. Click edit for the content row, since that’s what we will be doing in this article, and then set the permissions as shown in the screenshot below:
Now I’ll set proper permissions for our new authenticated user in order to let the user create, edit and delete content. I do this by navigating to admin/people/permissions. Set the following permissions:
- Basic Page: Create new content
- Basic Page: Delete own content
- Basic Page: Edit own content
Get User’s Token
Before we start creating a node, we need to get our new user’s token in order to pass authentication. This can be done by testing API calls. For testing API calls, I’ll use the Restlet Client – Rest API Testing extension for Chrome. Of course, if you prefer some other method, feel free to use that one instead.
Now, to test my API calls and get the new user’s token, I’ll first log out of my site as an admin and log in with the new user account. Now, I’ll simply copy the URL of my site, add rest/session/token at the end of it and paste it in the Restlet client’s URL field. Next, I’ll select the ‘GET’ method from the dropdown and send the URL to get the token from the body field. Here’s a screenshot from an earlier call:
Create Node and Test API Call
Now that I’ve got my unique token, I can start creating a node. To do so, the POST method is used to POST the entity/node, and the content-type should be set to application/hal+json. The title and type fields should be declared in the body field like this:
“value”:”My first page”
The following headers have to be added for this call:
Content-Type : application/hal+json
X-CSRF-Token : ‘The token that we got from the previous step.’
Click ‘Add authorization’ and enter the credentials of the authenticated user to add authentication if required.
This is what it should look like before firing the API call:
Next, go to your Drupal site and navigate to admin/content. Check to see if the node you created is visible on your site. If it’s there, your API call works.
That’s it! You’ve successfully created your first Drupal node using RESTful web services. Note that this is a very basic function of RESTful web services intended to give a demo of the feature.
Having trouble with your Drupal project? Stuck with customizing your Drupal site to your own liking? Lay aside your worries and hand them over to us at Agiledrop.