Hi guys,
I'm stuck on this weird issue on creating Rest APIs with Drupal 8.
The problem is that while I can make request to a GET endpoint and receive the correct response, the POST endpoint responds with an error message of "message": "No route found for \"POST /custom_practise_module/custom_api\": Method Not Allowed (Allow: GET, HEAD)"
Headers I passed with the requests includes Content-Type, X-CSRF-Token(got from rest/session/token), authorisation.
The REST resources have been enabled by REST UI for both the GET and POST methods.
The actual endponit is as following
public function get() {
$response = ['message' => 'Hello, this is a GET response!'];
return new ResourceResponse($response);
}
/**
* @return \Drupal\rest\ResourceResponse
* The HTTP response objects
*/
public function post() {
$response = ['message' => 'Hello, this is a POST response!'];
return new ResourceResponse($response);
}
I'm not sure where I got wrong and any help would be appreciated.
Thanks
Comments
/**
Same Problem
I am having the exact same problem. What am I doing wrong? I cannot see it
POST return "no route found" error while GET request is functio
I am facing same issue:
"message": "No route found for "POST /api/demo_resource": Method Not Allowed (Allow: GET, HEAD)"
Set the relation path
Please check if you have set the relation path like below:
uri_paths = {
* "canonical" = "/api/event_details/n_nid",
* "https://www.drupal.org/link-relations/create" = "/api/event_details/n_nid"
* }
----------
Warm Regards,
Ritu Raj
Colliding Paths
I hate to say it, but for me it ended up being something really simple. Back story... I created a custom Entity called Incident, and enabled the default REST resources that come with it (POST, GET, PATCH, DELETE). Which provides paths like /incident/1?_format=json, etc.
I then created a custom resource with GET and POST for a url of /incident/list, doing a GET worked fine on this, but doing the POST always ended up with the "no route found" error. Once I switch the canonical and link-relations paths to /list/incident, it worked immediately. So it looks like my first /incident/list path is/was somehow perhaps colliding with the POST /incident resource alongside the entity resource, instead of routing to my custom resource.
i am not able to understand
i am not able to understand can you share your code example here?
Hi!
Hi!
For those of you who care, I had the same problem after upgrading to Drupal 9, POST method for my REST resources didn't work anymore (404 error) when it still worked with Drupal 8.9.
I managed to fix the problem by replacing this line in the annotations of the resources :
"https://www.drupal.org/link-relations/create" = "/my_api/create_entity"
by this one :
"create" = "/my_api/create_entity"
Hope that this will help someone!
+1 This worked for me too!
+1 This worked for me too!
How to do that?
Hi gaspounet
I have the same problem I think. Where exactly to you find that resource? I'm still kind of new to Drupal.
I even tried to make a case: https://www.drupal.org/project/drupal/issues/3153769 but no good answer as come...
Hi,
Hi,
In order to build your custom REST API resource and to POST data, you first have to create a custom module and then create the following folder structure inside your module : "src/Plugin/rest/resource".
Inside this last folder you create your custom class (for example in a file "ResourceOfMyOwn.php" where you can put this kind of content (I return here a "ModifiedResourceResponse" object to send a JSON response):
You need to clear the DRUPAL cache and go to /admin/config/services/rest in order to activate your resource.
Hope I understood and correctly answered your question.
Thanks for your answer. I
Thanks for your answer. I think your example are more than I need?
I'm just trying to create instances of my content types. I't works in 8.9.3 but when I upgrade to 9.0.5 it give me no route found. Then I saw your post and thought that I my have the same problem?
similar error
Hi!
i am only create one node via rest, not custom module
i have no problem in Drupal 8.9 but after upgrade to Drupal 9 i get:
"message": "No route found for \"POST /entity/node\""
any idea?,
Thanks
Did you
Weird, did you clear the cache after upgrade or did you enabled the "Content" ressource on the /admin/config/services/rest page?
only when created
Hi gaspounet, thanks for response,.
Yes i clear cache and enabled the "Content" ressource on the /admin/config/services/rest
I can login, create and update profiles, get profiles ,get users, get node, but not create.
This issue describe similar problem.
Thanks.
+1 This worked for me too!
Yes... you save me too!!!. I was getting mad with this issue. Thanks.
Thank u
You saved My life
this my full worked example..:)
Wondering...
I wonder when this changed. sometime in the last year and half? Just found out on one my old custom modules that this was a problem and you helped! +1
Works like magic!
Thank you!
+1 This is still working in
+1 This is still working in D9.3.6.
I can confirm this fix is
I can confirm this fix is working as well. My full code example follows:
Still issue
Hello,
This is my code:
* Represents Custom API records as resources.
*
* @RestResource (
* id = "custom_api",
* label = @Translation("Custom API"),
* uri_paths = {
* "canonical" = "/api/custom-api/{id}",
* "create" = "/api/custom-api"
* }
* )
and still I am getting 404 in postman while hitting this url using GET method: https://mtn-drupal.ddev.site:8443/api/custom-api/22?_format=json. Permission is given also.
----------
Warm Regards,
Ritu Raj