How to access the request in REST API I'm trying to follow the example of a custom rest api in https://www.drupal.org/docs/8/api/restful-web-services-api/custom-rest-resources.

I don't understand how I can access the request for get or post. In the example there is only a default response, but I would of course like to make it depending on the request headers and body.
The only classes that extend ResourceBase implemented by Drupal core don't check/use what passed in the request. So I could not use Drupal core code as example.

If you really need to access any request value, and the argument passed to the get() or post() methods is not enough/adequate for your purpose, you can inject the request object in the create() static method.

  public function __construct(array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, LoggerInterface $logger, Request $request) {
    parent:: __construct($configuration, $plugin_id, $plugin_definition,  $serializer_formats, $logger);
    $this-&gt;request = $request;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration, 
      $plugin_id, 
      $plugin_definition, 
      $container-&gt;getParameter('serializer.formats'), 
      $container-&gt;get('logger.factory')-&gt;get('rest'),
      $container-&gt;get('request_stack')-&gt;getCurrentRequest()
    );
  }


In the class methods you then use $this-&gt;request to access the request object.