0

I am creating a custom rest resource.

/**
 * Provides a resource to update nodes.
 *
 * @RestResource(
 *   id = "my_custom_resource",
 *   label = @Translation("Custom rest resource"),
 *   uri_paths = {
 *     "canonical" = "/custom-api/{node}",
 *   }
 * )
 */
class MyCustomResource extends ResourceBase {

  public function patch(EntityInterface $entity, $data = []): ResourceResponse {
    return new ResourceResponse('Some message', 200);
  }

}

But when I try to send a patch request from my postman using the URL http://mysite/custom-api/2169, I am getting an error

RuntimeException: Callable "Drupal\my_module\Plugin\rest\resource\MyCustomResource::patch" requires a value for the "$entity" argument. in Drupal\Component\Utility\ArgumentsResolver->handleUnresolvedArgument() (regel 143 van /var/www/web/core/lib/Drupal/Component/Utility/ArgumentsResolver.php).

What can be the problem? How can I get the argument?

1 Answer 1

1

Your canonical url should be "/custom-api/{entity}"

3
  • still I get this error. RuntimeException: Callable "Drupal\paddle_webservice_api\Plugin\rest\resource\AddNodeTranslation::patch" requires a value for the "$entity" argument. in Drupal\Component\Utility\ArgumentsResolver->handleUnresolvedArgument() (regel 143 van /var/www/web/core/lib/Drupal/Component/Utility/ArgumentsResolver.php).
    – Ahmad
    Oct 25, 2021 at 5:29
  • If I do not use it as a object of EntityInterface then when I print the $entity variable, it prints the node id which I pass in the url.
    – Ahmad
    Oct 25, 2021 at 5:30
  • Right, it won't be the full entity, just the value passed, you should validate the value and load the entity in your patch function.
    – Lambic
    Oct 25, 2021 at 18:18

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.