Skip to content

Core\Request::json

Core\Request::json({maxDepth?: uint}): mixedCore\Request::json(
    {maxDepth?: uint},
): mixed

Reads the body of the request as one JSON document and returns the decoded value. A JSON object becomes an array with string keys, and a JSON list becomes a list. The result has the type mixed, so you convert each part with as to the type you need. Every string in it is tainted, which means it came from outside your program.

The body must be one complete JSON document. If it is not valid JSON, or if it is empty, json throws a ParseError. A web program usually answers that with the status 400. The Content-Type header of the request is not checked.

maxDepth limits how deeply the document may nest. The default is 512. Every call returns the same value, so you can call json more than once.

Options

  • maxDepthuintoptionalHow deep the document may nest. The default is 512, and the value must be from 1 to 1024. A document with no array or object in it has depth 1.

Return value

Returns mixed. The decoded value, the same as Core\Json::decode returns for the body. A JSON object becomes an array with string keys. Every string in it is tainted. Every call returns the same value.

Errors

  • LogicErrorThe program is not handling a request. Or bodyStream or files already read the body, and those two do not keep it. Or maxDepth is not from 1 to 1024.
  • ParseErrorThe body is not one whole JSON document, or it nests deeper than maxDepth. An empty body throws this error too. The Content-Type header is not checked.
  • RuntimeErrorThe body is larger than [limits] request_body. The bytes past that limit are never stored.
  • IOErrorThe connection failed while the body arrived, or the client sent fewer bytes than its Content-Length said.

Examples

Reading a json body

<?nvs
// Shows how to read the fields of a JSON document that a client sends in the request body.
// `Core\Request::json` returns the decoded body as a `mixed` value.
// A JSON object becomes an array with string keys.
var $user = Core\Request::json() as array<mixed>;
// `as` converts each field to the type you need. Every string in it is tainted.
tainted string $name = $user['name'] as tainted string;
int $age = $user['age'] as int;
// This prints "Ada is 36".
echo $name, ' is ', $age, "\n";
Output
Ada is 36

A body that is not json

<?nvs
// Shows how to answer a client whose request body is not valid JSON.
// The body below is missing its closing `}`. `Core\Request::json` throws a `ParseError`.
// This prints "400: the body is not valid JSON".
try {
var $data = Core\Request::json();
echo "200: ok\n";
} catch (ParseError $error) {
echo "400: the body is not valid JSON\n";
}
Output
400: the body is not valid JSON

Receiving an order

<?nvs
// Shows an API that receives an order as JSON and adds up its total price.
// The client sends a list of items. Each item has a price in cents and a count.
var $order = Core\Request::json() as array<mixed>;
var $items = $order['items'] as array<mixed>;
int $total = 0;
foreach ($items as mixed $entry) {
var $item = $entry as array<mixed>;
$total = $total + ($item['cents'] as int) * ($item['count'] as int);
}
// This prints "3 items, 3350 cents".
echo Core\Arr::count($items), ' items, ', $total, " cents\n";
Output
3 items, 3350 cents