Core\Test\Response::json
json() reads the body of the answer to a request that Core\Test::request() sent, as one JSON document, and returns the decoded value. A JSON object becomes an array with string keys. It works like Core\Request::json, which reads the body of a request.
If the body is empty or is not valid JSON, json() throws a ParseError. The Content-Type header is not checked. The maxDepth option sets how deep the document may nest. The default is 512, and the value must be from 1 to 1024. Any other value throws a LogicError.
You can call json() as often as you like. Each call decodes the body again.
The examples below show how to check values in a JSON answer, what happens with a body that is not JSON, and how maxDepth limits the nesting.
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 call decodes the body again and returns the same value.
Errors
LogicErrormaxDepthis not from 1 to 1024.ParseErrorThe body is not one whole JSON document, or it nests deeper thanmaxDepth. An empty body throws this error too. TheContent-Typeheader is not checked.RuntimeErrorThe body is not valid UTF-8, so it is not text.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Test::request has new options: clientIp, scheme, cookies, and the body options json, form, multipart and contentType of Core\Http\Client. The response is the one a server sends. A write from another site gets 403, and a HEAD request gets no body. |
Examples
Check values in a json answer
<?nvs// Shows how to check single values in the JSON that an API sends.
use Core\Http\Method;use Core\Test;
if (Core\Env::context() == Core\Env::Context::Request) { // This part answers each request. It sends one product as JSON. Core\Response::sendJson(['sku' => 'mug-7', 'price' => 12, 'tags' => ['blue', 'kitchen']]);} else { // This part is the test. var $response = Test::request(Method::Get, '/products/mug-7');
// `json()` returns the decoded body. A JSON object is an array with string keys. var $product = $response->json() as array<mixed>;
// This prints `mug-7 12 2`. echo $product['sku'], ' ', $product['price'], ' ', Core\Arr::count($product['tags'] as array<mixed>), "\n";}mug-7 12 2A body that is not json
<?nvs// Shows what `json()` does when the body of the answer is not JSON.
use Core\Http\Method;use Core\Test;
if (Core\Env::context() == Core\Env::Context::Request) { // This part answers each request. It writes an HTML page. echo '<h1>Not found</h1>';} else { var $response = Test::request(Method::Get, '/api/missing');
// An HTML page is not JSON, so `json()` throws a `ParseError`. // This prints `not JSON`. try { $response->json(); } catch (ParseError $error) { echo "not JSON\n"; }}not JSONLimit the nesting
<?nvs// Shows how the `maxDepth` option limits how deep the JSON may nest.
use Core\Http\Method;use Core\Test;
if (Core\Env::context() == Core\Env::Context::Request) { // This part answers each request. The JSON is an object inside an object, // so its depth is 3. Core\Response::sendJson(['order' => ['id' => 7]]);} else { var $response = Test::request(Method::Get, '/orders/7');
// A depth of 3 is allowed, so this prints `{"order":{"id":7}}`. echo Core\Json::encode($response->json({ maxDepth: 3 })), "\n";
// A depth of 2 is not enough, so `json()` throws a `ParseError`. // This prints `too deep`. try { $response->json({ maxDepth: 2 }); } catch (ParseError $error) { echo "too deep\n"; }}{"order":{"id":7}}too deep