Error message

Deprecated function: Array and string offset access syntax with curly braces is deprecated in include_once() (line 20 of /home/handsom1/public_html/hdogstudio/html/includes/file.phar.inc).

Updating a Drupal node using Services

We have been using Drupal's Services module for a website, allowing a process that creates content in one platform to create new nodes in the Drupal site whenever a new piece of content is created. Easy to accomplish; we found many examples online showing how to create nodes.

We have a new requirement for which the remote system needs to update one of the Drupal site's nodes. Turned out to be more tricky and the web had fewer examples of how to do it. Here's how we did it (remember that you need to request an X-CSRF-Token and maybe authenticate, depending on your content permissions):

 

Find the node that we need to update

The remote system doesn't have the node id for the piece of content that it needs to update, so we created a view (view machine name "find_content") with two contextual filters that returns one nid (the most recent one - sort by post date, desc). To retrieve the node id matching those two contextual filters, we used this call:

 
 
(Using two headers: the X-CSRF-Token and Content-Type: application/json)
The system returns the node object as a json array - so if you're storing the node in a variable called $node, the nid is available at $node['nid'].
 

Updating the node: "PUT" your call

Whereas we POST new nodes using Services, to update a node send your call as a PUT.
 
For a node with nid 1234, to update the Status field we used this call:
 
 
(Using two headers: the X-CSRF-Token and Content-Type: application/xml)
 
The body of our PUT call was some xml:
 
<?xml version="1.0" encoding="UTF-8"?>
<data>
    <field_status>
        <und is_array="true">
               <item>
                <value>[new_value]</value>
            </item>
        </und>
    </field_status>
</data>