1

I'm using Drupal 9.5 to build my api inside my custom module. When I test the API URL on the browser it show me a message

message: "No route found for "GET /api/v1/api_resource""
<?php

namespace Drupal\demo_api\Plugin\rest\resource;

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

/**
 * Provides the API resource for the mobile App
 *
 * @RestResource(
 *   id = "demo_api_resource",
 *   label = @Translation("Demo API Resouce"),
 *   uri_paths = {
 *      "canonical" = "/api/v1/api_resource/"
 *   }
 * )
 */
class ApiResource extends ResourceBase {

  public function get() {
    $response = ['message' => 'Hello, this is a rest service'];
    return new ResourceResponse($response);
  }

I did configure in REST UI to accept the GET request with cookie authentication.

What could be the issue here?

1 Answer 1

1

I just found out, the issue is caused by the forward slash at the end of the path.

I have to change "canonical" = "/api/v1/api_resource/" to this one "canonical" = "/api/v1/api_resource"

Once I removed the forward slash from the path, the API is response as expected when you request mydomain.com/api/v1/api_resource?_format=json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.