Been looking at this for a while now and can't seem to find a solution. I have a custom REST resource that implements a GET, POST, PATCH and DELETE method using basic authentication and json as the format. The GET, PATCH works, but on the POST I always get a 405 error back:
Client error: `POST http://api_host/rest/my_custom_path/foo/bar?_format=json` resulted in a `405 Method Not Allowed` response:
{"message":"No route found for \u0022POST \/rest\/my_custom_path\/foo\/bar\u0022: Method Not Allowed (Allow (truncated...)
I defined my custom resource as:
/**
* @RestResource(
* id = "custom_resource_id",
* label = @Translation("Custom resource"),
* uri_paths = {
* "canonical" = "/rest/my_custom_path/{arg1}/{arg2}"
* }
* )
*/
class ContentSynchronisationAPI extends ResourceBase {
...
}
This is how I call it from another module in an other website:
$client = \Drupal::httpClient();
$token = $client->get("{$api_host}/rest/session/token")->getBody();
$response = $client->post("{$api_host}/rest/my_custom_path/foo/bar", array('headers' => ['Content-Type' => 'application/json','X-CSRF-Token' => $token
],'auth' => array($username, $password),'body' => $json_decoded_data,'query' => ['_format' => 'json'
]
));
The user that is used has a role that has the "restfull POST custom_resource_id" permission set.
Is this a known issue with POST?