You are here

protected function DocumentExtractor::decodeRequestPayload in JSON:API Resources 8

Decodes a request payload.

Mostly a duplication from the JSON:API module.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request.

Return value

array The request payload decoded into a multi-dimensional array.

See also

\Drupal\jsonapi\Controller\EntityResource::deserialize()

1 call to DocumentExtractor::decodeRequestPayload()
DocumentExtractor::extractResourceObjectFromRequest in src/Unstable/DocumentExtractor.php
Decodes and builds a resource object from a request body.

File

src/Unstable/DocumentExtractor.php, line 181

Class

DocumentExtractor
Document extractor for requests.

Namespace

Drupal\jsonapi_resources\Unstable

Code

protected function decodeRequestPayload(Request $request) : array {
  $received = (string) $request
    ->getContent();
  if (!$received) {
    $relationship_field_name = $request->attributes
      ->get('_jsonapi_relationship_field_name');
    if ($request
      ->isMethod('DELETE') && $relationship_field_name) {
      throw new BadRequestHttpException(sprintf('You need to provide a body for DELETE operations on a relationship (%s).', $relationship_field_name));
    }
    else {
      throw new BadRequestHttpException('Empty request body.');
    }
  }

  // First decode the request data. We can then determine if the serialized
  // data was malformed.
  try {
    $decoded = $this->serializer
      ->decode($received, 'api_json');
  } catch (\UnexpectedValueException $e) {

    // If an exception was thrown at this stage, there was a problem decoding
    // the data. Throw a 400 HTTP exception.
    throw new BadRequestHttpException($e
      ->getMessage());
  }
  return $decoded;
}