Hello,
How I can create a post Resource that can accept values by the body, this is my code:
namespace Drupal\custom_rest\Plugin\rest\resource;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Psr\Log\LoggerInterface;
/**
* Provides a resource to get view modes by entity and bundle.
*
* @RestResource(
* id = "custom_rest_resource_user_check",
* label = @Translation("Custom User Check"),
* uri_paths = {
* "canonical" = "/api/v1/custom/user_check",
* "https://www.drupal.org/link-relations/create" = "/api/v1/custom/user_check"
* }
* )
*/
class CustomRestResourceUserCheck extends ResourceBase {
/**
* A current user instance.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* Constructs a Drupal\rest\Plugin\ResourceBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param array $serializer_formats
* The available serialization formats.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* A current user instance.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
array $serializer_formats,
LoggerInterface $logger,
AccountProxyInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->getParameter('serializer.formats'),
$container->get('logger.factory')->get('custom_rest'),
$container->get('current_user')
);
}
/**
* Responds to POST requests.
*
* Returns a list of bundles for specified entity.
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* Throws exception expected.
*/
public function post() {
// You must to implement the logic of your REST Resource here.
// Use current user after pass authentication to validate access.
if (!$this->currentUser->hasPermission('access content')) {
throw new AccessDeniedHttpException();
}
return new ResourceResponse("Implement REST State POST!");
}
}
Greetings.
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | 2800871-11.patch | 3.58 KB | dawehner |
| #9 | custom_rest.tar_.gz | 1.4 KB | rpayanm |
| #6 | Selection_059.png | 66.65 KB | rpayanm |
| #6 | Selection_058.png | 4.71 KB | rpayanm |
| #5 | Selection_057.png | 69.17 KB | rpayanm |
Comments
Comment #2
wim leersYou already posted an issue at #2799383: Create a post method in Drupal 8: route not found with the exact same title. Please write sane issue titles. That ensures you get better help. I now retitled that other issue. And retitling this one too.
Comment #3
wim leersYou must specify a
serialization_classin the annotation. See\Drupal\rest\Annotation\RestResource.Or, if there is no serialization class, then you can just do:
As you can see in
\Drupal\rest_test\Plugin\rest\resource\NoSerializationClassTestResource.Comment #4
rpayanmSorry for the title :(
Now I write a serialization class:
And in the annotation:
In the method:
But now I got this error for postman:

Comment #5
rpayanmComment #6
rpayanmAlready implement
serialization_class.Here my implementation:
My Normalize class:
My annotation class:
My services.yml
I have activated:

I tested this functionality in Postman but I get the following error:

Comment #7
dawehnerIf you want to make life easier, provide a module we can just download :)
Comment #8
wim leersserialization_class = “Drupal\custom_rest\Plugin\rest\annotation\TestResource”,This is not at all a serialization class. Look at the examples in Drupal core. The serialization class is the PHP object that this must be deserialized to from JSON, or is serialized from.
Comment #9
rpayanm@Wim Leers I changed the serialization class but dont work :(
I attached the module.
Thank you.
Comment #10
dawehnerYou misunderstood the concept of the serialization class, its the class you are trying to serialize to, not the class which does the normalization/denormalization.
The main problem was that
\Drupal\custom_rest\Normalizer\TestDenormalizerhad to implementDenormalizerInterfaceas well.Note: Personal contact emails are not meant for asking for support.
Comment #11
dawehnerHere is a patch for your module which also moves stuff around.
Comment #12
rpayanmThank you so much @dawehner
Comment #13
wim leersIndeed. @rpayanm also contacted me.
You're already getting support here.
Comment #14
dawehnerEspecially don't expect people to react in any timeframe where you need it. If you need payed support times, pay for it :)