Updating content via REST endpointsfor Drupal 8 , 9 , 10 , and 11

Last updated :  

In a dynamic web environment, the ability to update existing content via REST endpoints is crucial. It empowers you to maintain content accuracy and relevance, automate content changes, and provide timely updates to users. In this lesson, we explore how to efficiently update content on a Drupal site using RESTful endpoints.

Importance of Content Updates via API

Updating content programmatically through REST APIs allows seamless integration with other systems and third-party services. This capability is essential for managing content lifecycles and deploying content updates efficiently across different platforms.

Prerequisites

Ensure that you have the following in place:

  • An understanding of Drupal's RESTful setups as discussed in our earlier lessons.
  • The RESTful Web Services, Serialization, and relevant authentication modules (e.g., cookie, OAuth) must be enabled and configured.
  • Ensure permissions are set to allow content updates via REST endpoints.

Configuring the REST Endpoint for Updates

To update content, configure your REST endpoint to handle PATCH requests. Here’s how:

Step 1: Enable the Patch Method for Content

Navigate to Configuration > Web services > REST. Under the Content resource, enable the PATCH method:


    resources:
      entity:node:
        PATCH:
          supported_formats:
            - json
          supported_auth:
            - basic_auth
            - cookie
            - oauth
    

Step 2: Ensure Appropriate Permissions

Go to People > Permissions, and ensure that roles intended to update content through the API have permission to update content and access RESTful PATCH operations.

Making an API Request to Update Content

To update existing content, identify the content entity by its ID, and prepare a PATCH request with the updated data.

Example: Updating an Article Node

Let’s assume an article node with an ID of 1 exists, and you wish to update its title and body. Prepare the JSON payload as follows:


    {
        "title": [{"value": "Updated Article Title"}],
        "body": [{
            "value": "This is the updated body of the article.",
            "format": "plain_text"
        }]
    }
    

Sending the PATCH Request

Use a tool like curl or Postman to send a PATCH request, including authentication information:


    curl -X PATCH http://your-drupal-site/entity/node/1 \
    --header "Content-Type: application/json" \
    --header "Authorization: Bearer [your_access_token]" \
    --data '[YOUR_JSON_PAYLOAD]'
    

This command updates the specified article node, changing its title and body.

Handling API Responses

Examine the API response to confirm that the update was successful:

  • A successful update typically returns a status of 200 OK with the updated entity data in the response.
  • Use the updated entity data to verify changes were applied correctly.

Debugging Common Issues

  • 401 Unauthorized: Ensure your authentication credentials are correct and active.
  • 403 Forbidden: Check your permissions to confirm that the user role has access to update content.
  • 404 Not Found: Verify the resource URL and ID exist on the system.

Conclusion

Understanding how to update content via REST endpoints is essential for maintaining dynamic and current content within your Drupal application. This capability, combined with authentication and permissions setups, ensures that updates are made securely and efficiently.

Preview of Next Lesson

In the next lesson, we’ll delve into Deleting Content via REST Endpoints. Learn how to remove content programmatically using RESTful services, which is crucial for content lifecycle management and automation in headless setups. Stay tuned to further empower your headless Drupal applications!