Score:1

Rest API show No route found for GET error

th flag
fin

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?

Score:1
th flag
fin

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

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.