How to create a node with REST in Drupal 8

How to create a node with REST in Drupal 8

Creating a node with REST has never been this easy in Drupal 8, in this blog post I'll show you how to do it.

The first thing you would need to do is enable all the required modules (/admin/modules).

REST modules
Enable the required REST modules

You will notice I use the REST UI module. By using this module we don't have to edit .yml files to enable resources, instead everything is configurable in the UI.

Before you can POST a node you have to enable /entity/node or /node in the resources and enable the POST resource with JSON and Basic Auth (/admin/config/services/rest/resource/entity:node/edit).

Node POST resource
Enable the POST resource with basic auth

The last step is to have an actual POST call to you Drupal back-end:

REST create node
POST call in Postman

In this example we create a basic page with a title and body. Also make sure to add an authorization header with your credentials:

Node POST resource auth
Add an authorization header in POST call in Postman

In this example the Authorization header is a Base64 encode of our user and password (admin/admin).

When we fire this post we will see the node was created in our Drupal back-end:

Node POST resource result
The node we created over REST

That is it for now, I hope this tutorial helped. If you have any questions, don't hesitate to leave a comment below.