1

In Drupal, we can create nodes with the core module "RESTful Web Services". There is a lot of documentation about creating one node with one request, the Drupal documentation provides the following example:

curl --include \
  --request POST \
  --user klausi:secret \
  --header 'Content-type: application/hal+json' \
  --header 'X-CSRF-Token: <obtained from http://example.com/rest/session/token>' \
  http://example.com/entity/node?_format=hal_json \
  --data-binary '{"_links":{"type":{"href":"http://example.com/rest/type/node/article"}},"title":[{"value":"Example node title"}],"type":[{"target_id":"article"}]}'

We want to import 25.000 nodes on a regular basis and worry that this could take really long if we use a separate request for each node.

It should be possible to send a list of nodes. Drupal's REST web service uses the Serializer Component from the Symphony framework. The Symphony docs say that it includes the serialization of lists of nodes. Passing a list to the REST web service should simply look like this.

curl --include \
  --request POST \
  --user klausi:secret \
  --header 'Content-type: application/hal+json' \
  --header 'X-CSRF-Token: <obtained from http://example.com/rest/session/token>' \
  http://example.com/entity/node?_format=hal_json \
  --data-binary '[{"title":[{"value":"Title 1"}],"type":[{"target_id":"article"}]},{"title":[{"value":"Title 2"}],"type":[{"target_id":"article"}]}]'

When I send this data, I get this response.

Could not determine entity type bundle: type field is missing.

t seems that the serializer does not recognize that I send a list.

What is the correct way to send a node list to the REST API?

2
  • 1
    It doesn't appear to be supported - if you look at Drupal\rest\Plugin\rest\resource\EntityResource, all of the code and supporting comments suggest you can only create one entity at a time. Indeed the only parameter to the post() method is EntityInterface $entity, rather than an array. Maybe you can impose some middleware in the way which changes that, or perhaps write your own resource
    – Clive
    Commented Feb 21, 2019 at 15:07
  • 1
    Maybe of interest: drupal.org/project/subrequests
    – Clive
    Commented Feb 21, 2019 at 16:41

1 Answer 1

5
+50

Solution 1: Custom Endpoint

Unfortunately, entities are expected to be created one by one when using RESTful web services. You can create your own web service endpoint by writing a custom module and there, you can create support for creating multiple nodes with a single request. However, you'll still have to use batches of say, 50 nodes per request for things to work smoothly. This is more of a push import.

You might also want to try the jsonapi module to see if supports importing multiple operations in a single request.

Solution 2: Migrate API

Have you considered Drupal's migrate API instead? With migrate, you can import nodes from a remote XML / JSON document with a simple drush command which you can setup as a part of cron on your server. You can update existing items and create non-existing items. Migrate will also create nodes one by one, but it will be much faster than REST requests because it will not involve 1 HTTP request per node. This is more of a pull import.

1
  • You can also use the drupal jsonapi module . Commented Feb 27, 2019 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.