Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Request.php

function Request::toArray

Gets the request body decoded as array, typically from a JSON payload.

Throws

JsonException When the body cannot be decoded to an array

See also

getPayload() for portability between content types

File

vendor/symfony/http-foundation/Request.php, line 1479

Class

Request
Request represents an HTTP request.

Namespace

Symfony\Component\HttpFoundation

Code

public function toArray() : array {
    if ('' === ($content = $this->getContent())) {
        throw new JsonException('Request body is empty.');
    }
    try {
        $content = json_decode($content, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
    } catch (\JsonException $e) {
        throw new JsonException('Could not decode request body.', $e->getCode(), $e);
    }
    if (!\is_array($content)) {
        throw new JsonException(\sprintf('JSON content was expected to decode to an array, "%s" returned.', get_debug_type($content)));
    }
    return $content;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal