Score:0

How to use patch request with parameters in custom rest resource?

kr flag

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?

Score:1
ph flag

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

kr flag
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).`
kr flag
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.
Lambic avatar
ph flag
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.
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.