Need to migrate Rest services custom module, that processes POST requests, from drupal 7 to 8. The GET works by following instructions in https://www.drupal.org/docs/8/api/restful-web-services-api/custom-rest-r.... But can't get POST to work, route not found error.
The GET is working, but POST give path error.
drupal-8.8.4
---
<code>
/modules/custom/tax_tob/config/install/rest.resource.demo_resource.yml
id: Demo_resource
plugin_id: Demo_resource
granularity: method
configuration:
GET:
supported_formats:
- json
supported_auth:
- basic_auth
POST:
supported_formats:
- json
- hal_json
supported_auth:
- basic_auth
---
/modules/custom/DemoApi/src/Plugin/rest/resource/DemoResource.php
<?php
namespace Drupal\DemoApi\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" = "/DemoAPi/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 GET'];
return new ResourceResponse($response);
}
/**
* Responds to entity POST requests.
* @return \Drupal\rest\ResourceResponse
*
* @param mixed $data
* Data payload.
*/
public function post(Array $data) {
$response = ['message' => 'Hello, this is a rest service POST'];
return new ResourceResponse($response);
}
}
</code>
error:
'No route found for "POST /DemoApi/Demo_resource"'
DEBUG:requests.packages.urllib3.connectionpool:"POST /DemoAPi/Demo_resource?_format=json HTTP/1.1" 404 76
Rcv payload : {u'message': u'No route found for "POST /DemoAPi/Demo_resource"'}
Comments
add create uri_paths
Add create path to class comment after canonical
That is it
I got stuck for 30min because of the same issue, glad I found your answer
Technical Architect at Acquia