drupal-KW-rest-srv

In this tutorial we’ll review how to use Drupal 8 Restful Web Services. We’ll take you through viewing, creating, editing, and updating entities for your Drupal site. (Note: I am using Beta 14 version in this tutorial.)

Alright, let’s begin!

Enabling Modules

Before you start using RESTtful Web Services, enable the following modules: HAL, HTTP Basic Authentication, and RESTful Web Services and Serialization.

Go to /admin/modules or click “Extend” on the menu.1_module_install

Permissions

Next, go to the permission page(/admin/people/permissions) and select the checkbox in the RESTful Web Services section to allow your users to make the API call. For example, I’m allowing all anonymous users to access my content by using the GET request API.

2_permission

Installing REST UI Module

Next, install the Rest UI module(https://www.drupal.org/project/restui ) to configure the request method. This module allows you to enable the API request in the admin page directly.

After you install the Rest UI module, proceed to the REST module page in the configuration page (admin/config/services/rest). You should be able to see all the API requests available.

3_restui

In addition to node content, you can enable the API request for other resources such as user, comment, or your custom content type. Click the Enable/Edit button and select hal_json and json for supported formats and basic_auth for authentication providers.

4_restui2

Testing the API

Now we’re ready to use RESTful Clients to test our API. I am using “DHC – REST/HTTP API Client ” on Chrome. You can use “Postmen” or any REST clients you like.

The Postmen plugin is fairly friend to use. You set the url, request type, header and body. Then you can send your request out.

You need to add an authentication header if you don’t give permission to anonymous users. Click “set an authorization” and input the username and password you use in your Drupal site.

GET (get my first node)

For example, I am getting the first node of my site by using GET method with the following URL: localhost/beta14/node/1?_format=json

5_get

Make sure you the green “OK” message on the left is visible. You may need the X-CSRF-Token header for some request. Get the token from the link: http://example.com/rest/session/token

Here’s the information for the other requests like POST, PATCH and DELETE.

POST  (create a new node)

url: localhost/beta14/entity/node

body: {“_links”:{“type”:{“href”:”http://localhost/beta14/rest/type/node/page“}}, “title”:[{“value”:”my page”}], “body”:[{“value”:”page body”}]}

headers:

Content-Type: application/hal+json

Accept: application/json

X-CSRF-Token: {your token}

6_post

PATCH (update node’s title and body)

url: localhost/beta14/node/6

body: {“_links”:{“type”:{“href”:”http://localhost/beta14/rest/type/node/page“}}, “nid”:[{“value”:”6″}],”title”:[{“value”:”new title”}], “body”:[{“value”:”updated”}]}

headers:

Content-Type: application/hal+json

Accept: application/json

X-CSRF-Token: {your token}

7_patch

DELETE (delete node with id=5)

url: localhost/beta14/node/5

headers:

Content-Type: application/hal+json

Accept: application/json

X-CSRF-Token: {your token}

8_delete

Conclusion:

Now you should be able to create RESTful Web Services from your Drupal 8 CMS. You can start calling the service from your mobile app or website. If you add a new service to your Drupal site, make sure you give the proper permissions to the user group and configure the endpoints by using the REST UI module. Best of luck!