This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

📣 Join our live AMA on the Future of Page Builders with Brizy's CEO! Register Now →

How To Create A Node In Drupal 8 Using RESTful Web Services

Updated on January 22, 2018

4 Min Read
create-drupal-8-node-using-restful-web-services_Banner

RESTful Web Services is an incredibly powerful web tool that enables external resources to interact with applications. With the introduction of Drupal 8, RESTful Web Services was integrated in the Drupal core. This enables Drupal to be extremely flexible and opens up a host of possibilities. In this tutorial, I will introduce you to the web services of Drupal using RESTful Web Services to create a node in Drupal.

Enable Modules

Get started by enabling the following 4 core modules in Drupal:

  • HAL
  • HTTP Basic Authentication
  • RESTful Web Services
  • Serialization

I will also be downloading the Rest UI module for Drupal 8. This allows easy settings of permissions and settings for the Restful services through a GUI, instead of going through the rest.settings.yml file.

create drupal 8 node using restful web services

Set Permissions

Next, create a new authenticated user for your site. This way you’ll learn about the kind of permissions that should be set. Alternatively, if you were to be logged in as an admin, you would automatically have all the permissions.

So, go ahead and create a new user. Once done, go to admin/configuration/web services/REST . Here you can enable permissions for every type of resource on your Drupal site. For now, since we’re only concerned with creating content through the RESTful Web Services, go ahead and click edit for the content row and set the following permissions:

create drupal 8 node using restful web services 2

Next, we need to set the proper permissions for authenticated users to be able to create, edit and delete content. Go to admin/people/permissions and set the following permissions:

  • Basic Page: Create new content
  • Basic Page: Delete own content
  • Basic Page: Edit own content

create drupal 8 node using restful web services

Get Token

We are now ready to use REST to create a new node (page content type) in our Drupal site. Note that I will be using the excellent Restlet Client – Rest API Testing extension for chrome to test the API calls in this post. You can use whichever method you prefer.

Before we start creating a node for our site, we will have to get the token for our user in order to pass authentication. Do this by logging out of your site as an admin and logging in as the new authenticated user that we created in the last step. Now copy the URL of your site and paste it in the URL field of the Restlet Client, adding rest/session/token at the end of it. Select the ‘GET’ method from the dropdown and click send to get the token from the body field.

Nothing as Easy as Deploying Drupal Apps on Cloud

With Cloudways, you can have your PHP apps up and running on managed cloud servers in just a few minutes.

create drupal 8 node using restful web services

Create Node

Now that we have the unique token for our user, we can get started with creating a node for our Drupal site using RESTful Web Services. We will be using the POST method to POST to entity/node, setting the Content-Type to application/hal+json. In the body field, we must declare the required title and type fields in the following way:

 { 

   "_links":{ 

     "type":{ 

       "href":"http://drupal-121194-391381.cloudwaysapps.com/rest/type/node/page" 

     } 

   }, 

   "title":[ 

     { 

     "value":"My first page" 

     } 

     ] 

 } 

We will have to add the following headers for this call:

  • Content-Type : application/hal+json
  • X-CSRF-Token : ‘The token that we got from the previous step’

To add authentication, click add authorization and enter the credentials of the user that we created earlier.

create drupal 8 node using restful web services

Here’s what it looks like:

create drupal 8 node using restful web services

Click send and you should receive the 201 created response.

Verify Rest API Call

Now go to your Drupal site and navigate to admin/content . Your API call works perfectly if you see a new node created with the Title that you gave in your API call.

create drupal 8 node using restful web services

Conclusion

That’s it! You’ve successfully created your very own Drupal node by using the RESTful Web Services of Drupal. In this tutorial, we used the POST method. You can use the same procedure to GET, POST, PATCH and DELETE nodes in Drupal as well. Hope you found this guide useful. If you face any issues, leave me a comment and I’ll get back to you.

If you are looking to create nodes in Drupal 7, see our previous blog post on: How To Programmatically Create Nodes In Drupal 7

Share your opinion in the comment section. COMMENT NOW

Share This Article

Hamza Zia

Hamza is a Drupal Community Manager at Cloudways - A Managed Drupal Hosting Platform. He loves to write about Drupal and related topics. During his free time, he can be seen obsessing over Football, Cars, Android and Gaming.

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now