Activating the core rest modulefor Drupal 8 , 9 , 10 , and 11

Last updated :  

Introduction

In this lesson, we'll focus on the activation and configuration of the Core REST module in Drupal, a foundational element for building headless applications. This module expands Drupal's capabilities as a content repository by exposing RESTful web services, thus enabling interaction with diverse front-end clients and other applications.

What is the Core REST Module?

The Core REST module allows different client applications to interact with Drupal through RESTful API endpoints. Unlike the JSON:API module, which implements the JSON:API specification out-of-the-box, the REST module provides more granular control over each endpoint’s configuration, helping you tailor it to specific needs and formats such as JSON.

Benefits of Using the REST Module

The REST module offers several advantages:

  • Flexibility: Customize and configure specific endpoints for your entities, supporting various formats and operations.
  • Interoperability: Integrate Drupal with a wide range of technologies via RESTful APIs, including IoT devices, mobile apps, and custom web applications.
  • Adaptability: Enable and disable endpoints easily, ensuring that only needed data is accessible.

Steps to Activate the Core REST Module

Step 1: Enable the Module

First, activate the Core REST module. You can perform this action via the Drupal administrative interface or using the command line:

  • Using Drush:
  • drush en rest -y
  • Using the Admin Interface:
    1. Navigate to Extend in the admin toolbar.
    2. Search for RESTful Web Services and check the box beside it.
    3. Click Install at the bottom of the page to activate the module.

Step 2: Configure RESTful Endpoints

After enabling the module, configure the endpoints:

  1. Go to Configuration > Web services > REST.
  2. Here, you can enable HTTP methods (GET, POST, PATCH, DELETE) and specify fields for each entity type, such as content types and users.
  3. Select the appropriate format for each method, typically JSON, and adjust permissions as needed.

Testing the REST Endpoints

Once configured, testing your endpoints is essential to ensure they’re responding as expected. Use tools such as Postman or curl for verification:

  • Get Request Example:
    curl http://example.com/entity/node/1?_format=json
  • Post Request Example:
    
    POST /entity/node?_format=json
    Content-Type: application/json
    Authorization: Bearer {token}
    
    {
      "type": [{"target_id": "article"}],
      "title": [{"value": "New Article"}],
      "body": [{"value": "This is the body content."}]
    }
                        

Security and Permissions

Configuring correct permissions is crucial to safeguard your data. Navigate to People > Permissions and ensure appropriate roles are granted access to the REST operations in line with your security policies.

Advanced Configuration Strategies

For enhanced functionality, consider the following advanced strategies:

  • Use the Services module: For more complex scenarios requiring additional flexibility beyond Core REST.
  • Create custom REST resources: We will cover this in the upcoming lesson, "Defining REST plugins with RestResource," to tailor the REST API for specific use cases.
  • Utilize OAuth or JWT: For secure authentication, especially when exposing endpoints publicly.

Conclusion and Teaser for the Next Lesson

By activating and configuring the Core REST module, you empower your Drupal setup as a robust back-end solution for diverse client applications, enhancing its role in headless architectures. Ensuring precise configuration and secure implementation maximizes the module's benefits.

In the next lesson, we'll explore "Defining REST plugins with RestResource," providing a more in-depth approach to customizing your Drupal RESTful services. Stay tuned to expand your headless Drupal development horizon!