I had a custom restapiREST API working with Drupal 8 perfectly, after update. After upgrading to Drupal 9.2.X., the PatchPatch and PostPost methods of this custom rest api doesn't works anymore, gettingREST API stopped working. Now they throw the following error log:exception.
RuntimeException: Callable "Drupal\my_module\Plugin\rest\resource\UpdateBoxes::patch" requires a value for the "$payload" argument. in Drupal\Component\Utility\ArgumentsResolver->handleUnresolvedArgument() (line 143 of /var/www/docroot/core/lib/Drupal/Component/Utility/ArgumentsResolver.php).
RuntimeException: Callable "Drupal\my_module\Plugin\rest\resource\UpdateBoxes::patch" requires a value for the "$payload" argument. in Drupal\Component\Utility\ArgumentsResolver->handleUnresolvedArgument() (line 143 of /var/www/docroot/core/lib/Drupal/Component/Utility/ArgumentsResolver.php).
The code that i had for this restapi was:I am using is the following one.
namespace Drupal\my_module\Plugin\rest\resource;
use Drupal\rest\ModifiedResourceResponse;
/**
* Provides a resource to get view modes by entity and bundle.
*
* @RestResource(
* id = "update_boxes",
* label = @Translation("Update boxes"),
* uri_paths = {
* "canonical" = "/api/container/{container_id}/{type}/{product_id}/boxes"
* }
* )
*/
class UpdateBoxes extends ResourceBase {
/**
* A current user instance.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
protected $boxesHandler;
protected $event_dispacher;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->currentUser = $container->get('current_user');
$instance->boxesHandler = $container->get('boxes.handler');
$instance->event_dispacher = $container->get('event_dispatcher');
return $instance;
}
/**
* Responds to PATCH requests.
*
* @param $container_id
* @param $type
* @param $product_id
* @param $payload
* @return \Drupal\rest\ModifiedResourceResponse
* The HTTP response object.
*/
public function patch($container_id = null, $type = null, $product_id = null, $payload) {
//My code here.
}
}
namespace Drupal\my_module\Plugin\rest\resource;
use Drupal\rest\ModifiedResourceResponse;
/**
* Provides a resource to get view modes by entity and bundle.
*
* @RestResource(
* id = "update_boxes",
* label = @Translation("Update boxes"),
* uri_paths = {
* "canonical" = "/api/container/{container_id}/{type}/{product_id}/boxes"
* }
* )
*/
class UpdateBoxes extends ResourceBase {
/**
* A current user instance.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
protected $boxesHandler;
protected $event_dispacher;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->currentUser = $container->get('current_user');
$instance->boxesHandler = $container->get('boxes.handler');
$instance->event_dispacher = $container->get('event_dispatcher');
return $instance;
}
/**
* Responds to PATCH requests.
*
* @param $container_id
* @param $type
* @param $product_id
* @param $payload
* @return \Drupal\rest\ModifiedResourceResponse
* The HTTP response object.
*/
public function patch($container_id = null, $type = null, $product_id = null, $payload) {
//My code here.
}
}
And in the patch method iIn patch()
I could read the variable $payload$payload
and see the array sent in the patch request, but after runupgrading the Drupal 9 update $payloadsite, $payload
is NULLNULL
.
The solution that i appliedI found was changechanging the last argument "$payload"of patch()
and use the request to extract the payload from the $request$request
object.
public function patch($container_id = null, $type = null, $product_id = null, Request $request) {
//My code here.
$payload = json_decode($request->getContent(), TRUE);
}
public function patch($container_id = null, $type = null, $product_id = null, Request $request) {
// …
$payload = json_decode($request->getContent(), TRUE);
}
But iI couldn't find any documentation about this change, and i don'tchange; I am not sure that it was a good practice or the better implementation to get the payload working with patch rests.
I would appreciate if you know something about it and if this solution helped you.