How to create a node with REST in Drupal 8

Creating a node with REST has never been this fast and easy!

It's quite easy to create a node with REST in Drupal 8, in this blog post I'll show you how to do it.

First you will have to setup your REST resources. Make sure you enabled all modules:

Enable REST modules
Enable REST modules

I use the REST UI module and I recommend using it yourself because it's very handy. You don't have to edit the .yml files to enable your resources, instead you can do it with a UI.

Before you can post a node you have to enable /entity/node or /node in the resources and enable the POST with JSON and Basic Auth.

REST enable node
Enable the POST functionality for your content

Now you are all set to post content. Here is an example posting a node of type page from an Angular app:

    createPage: function (title, body) {
        var defer = $q.defer();

        page = {
          type: [
            {'target_id': 'page'}
          ],
          title: [
            {'value': 'A test title'}
          ],
          body: [
            {'value': 'A test body'}
          ]
        }

        $http({
          method: 'POST',
          url: config.endpoint + '/entity/node?_format=json',
          data: activity,
          headers: {
            'X-CSRF-Token': localStorage.get('csrf_token'),
            'Authorization': 'Basic XXXXXXXX',
          }
        })
          .success(function (data, status, headers, config) {
            defer.resolve(data);
          })
          .error(function (data, status, headers, config) {
            defer.reject(data);
          });

        return defer.promise;
      }

I hope this helps! If you have a problem please leave a comment on this page or contact me.

Drupal

Meer leesvoer

I have been working on a Drupal 8 multisite and at some point I had to set up a new site in the structure. The...
Drupal
In this blog post you will learn how to add a theme field preprocess function in your theme.
Theming
In this blog post I show you how to login and logout from a Drupal 8 Back-end from an Ionic app
Drupal
Drupal 8.3-x-dev is out and it comes packed with improvements to the REST module. It's now possible to create new users using REST.
Drupal