Custom REST Resource Results in "Permission is required"

Created on 11 April 2019, over 5 years ago
Updated 13 March 2024, 10 months ago

I followed this tutorial -> https://www.drupal.org/docs/8/api/restful-web-services-api/custom-rest-r... β†’ to set up a customer REST endpoint.

Here is my module

<?php

namespace Drupal\demo_rest_api\Plugin\rest\resource;

use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;

/**
 * Provides a Demo Resource
 *
 * @RestResource(
 *   id = "demo_resource",
 *   label = @Translation("Demo Resource"),
 *   uri_paths = {
 *     "canonical" = "/demo_rest_api/demo_resource"
 *   }
 * )
 */
class DemoResource extends ResourceBase {

  /**
   * Responds to entity GET requests.
   * @return \Drupal\rest\ResourceResponse
   */
  public function get() {
    $response = ['message' => 'Hello, this is a rest service'];
    return new ResourceResponse($response);
  }
}

Here is my rest config:

langcode: en
status: true
dependencies:
  module:
    - basic_auth
    - node
id: demo_resource
plugin_id: demo_resource
granularity: resource
configuration:
  methods:
    - GET
  formats:
    - json
  authentication:
    - basic_auth

My module is installed and its rest config was loaded into the database.

On the native Permissions screen, a new permission called "Access GET on Demo Resource resource" appeared in the "RESTful Web Services" section.

That permission is now enabled for authenticated users.

Nevertheless, making a GET request to the defined endpoint results in 403 Forbidden response status and the following response body:

{
    "message": "The 'restful get demo_resource' permission is required."
}

I am sending basic auth credentials and the CSRF token. FWIW, I have also enabled REST services for the built in Page entity, and requests I make to those endpoints, which use the same auth headers, are working.

Is there perhaps some other permission that needs to be flipped? I've been reading a few other bug reports and issue related to this, but no common theme or fix. I appreciate any help you can offer.

πŸ’¬ Support request
Status

Fixed

Version

10.1 ✨

Component
RESTΒ  β†’

Last updated about 1 month ago

Created by

πŸ‡ΊπŸ‡ΈUnited States sethgreen

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024