Hello,

I've been trying to create a post method in Drupal 8, but without positive result.

I even searched the core code, in Drupal Answers, in Drupal Documentation.

Here my issue:
http://drupal.stackexchange.com/questions/214706/create-a-post-method-in...

Someone may know that something is wrong?

Greetings.

CommentFileSizeAuthor
#3 Selection_054.png73.26 KBrpayanm
#3 Selection_053.png5.1 KBrpayanm
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rpayanm created an issue. See original summary.

Wim Leers’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: -REST, -REST API

Please export the relevant configuration and post it here.

rpayanm’s picture

FileSize
5.1 KB
73.26 KB

This is my code:

custom_rest/src/Plugin/rest/resource/CustomRestResourceUserCheck.php:


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"
 *   }
 * )
 */
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!");
  }

}

I have activated:
REST

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

rpayanm’s picture

Status: Postponed (maintainer needs more info) » Active
rpayanm’s picture

Drupal version: 8.1.9

Wim Leers’s picture

Status: Active » Postponed (maintainer needs more info)

Thanks for your code. But I asked for your configuration:

Please export the relevant configuration and post it here.

rpayanm’s picture

Sorry, you mean this:

resources:
  'entity:node':
    GET:
      supported_formats:
        - hal_json
        - json
      supported_auth:
        - basic_auth
    POST:
      supported_formats:
        - hal_json
        - json
      supported_auth:
        - basic_auth
    DELETE:
      supported_formats:
        - json
      supported_auth:
        - basic_auth
    PATCH:
      supported_formats:
        - json
      supported_auth:
        - basic_auth
  custom_rest_resource_user_check:
    POST:
      supported_formats:
        - json
      supported_auth:
        - basic_auth
link_domain: null
_core:
  default_config_hash: E9VXRiWZNet4YVBv8j9WQmTlgb-rOjo0MiCSdgV0Guw
langcode: es
rpayanm’s picture

Status: Postponed (maintainer needs more info) » Active
dawehner’s picture

You need a different path:

 * @RestResource(
 *   id = "custom_rest_resource_user_check",
 *   label = @Translation("Custom User Check"),
 *   uri_paths = {
 *     "https://www.drupal.org/link-relations/create" = "/api/v1/custom/user_check"
 *   }
 * )

why? I have no idea. Its some weird RFC stuff going on, in which world this makes sense.

Wim Leers’s picture

Status: Active » Fixed

Right. This will become more clear once #2293697: EntityResource POST routes all use the confusing default: use entity types' https://www.drupal.org/link-relations/create link template if available lands, because then there will be a bunch of examples in core. Will land in 8.3.x.

Wim Leers’s picture

Title: Create a post method in Drupal 8 » Create a post method in Drupal 8: route not found

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

squinternata’s picture

I have the same error not solved!!!
please help!!
A

cilefen’s picture

@squinternata Please open a new issue and link it to this one.