Node CRUD using Drupal’s web services and jQuery

If you still don't have the Web Servces and Rest UI modules on your Drupal web site, you can see how to add them at web services.

If you do have them, let's see how to CRUD a node using Drupal’s Web Services. You have to enable Get, Create, Update and Delete node on /admin/webervices/restui.
Also enable hal_json and json format, and authentication with cookie:

REST services

1. Create node (POST)

To create a node using the API, you'd first want to approve a permission for authenticated users to create nodes (/admin/people/permissions):

create content permission

CSRF token for the current user, and body in the JSON format. You can get CSFR token from /rest/session/token for the currently logged in or anonymous user:

  1. // Get token for the current user
  2. $.get('http:// yourwebsite/rest/session/token').done(function(response) {
  3.     var csrfToken = response;
  4. });

For creating a node you need to send headers (Content-Type': 'application/hal+json', 'Accept': 'application/json' and 'X-CSRF-Token': csrfToken) using a POST method to '/node?_format=hal_json'.

The body JSON data is like this:

  1. var jsonData = {
  2.     "_links": {
  3.         "type": {
  4.             "href": "http:// yoursite/rest/type/node/article"
  5.         }
  6.     },
  7.     "title": [ {
  8.         "value": "nodeTitle"
  9.     } ],
  10.     "body": [ {
  11.         "value": "nodeBody"
  12.     } ]
  13. }

If the call is successful, you'll need to receive the next result:

response

2. Get Node (GET)

For fetching a node you'll want to use only a node id and hal+json format:

  1. // Get a node using node id
  2. $.get("http:// yourwebsite/node/{nid}?_format=hal_json").done(function(response) {
  3.     var nodeJson = response;
  4. });

3. Update Node (PUT)

For node update you'll want to approve a permission for authenticated users to modify their own nodes, and you'll also need a node id and a CSRF token for the current user. Send headers ('Content-Type': 'application/hal+json', 'Accept': 'application/json' and 'X-CSRF-Token': csrfToken) and a JSON body to /node/{nid}?_format=hal_json' using the PUT method.

  1. var jsonData = {
  2.     "_links": {
  3.         "type": {
  4.             "href": "http:// yourwebsite /rest/type/node/article"
  5.         }
  6.     },
  7.     "title": [ {
  8.         "value": "nodeTitle"
  9.     } ],
  10.     "body": [ {
  11.         "value": "nodeBody"
  12.     } ]
  13. }

4. Delete Node (DELETE)

For deleting a node you'll want to approve a permission for authenticated users to delete their own nodes, and you'll also need a node id and a CSRF token for the current user. Send headers ('Content-Type': 'application/hal+json', 'Accept': 'application/json' and 'X-CSRF-Token': csrfToken) and a JSON body to /node/{nid}?_format=hal_json' using the DELETE method.

  1. var jsonData = {
  2.     "_links": {
  3.         "type": {
  4.             "href": "http:// yourwebsite/rest/type/node/article"
  5.         }
  6.     }
  7. }

If you want to try the code from this tutorial, you can download it from here.


Boldižar Santo

back to top