diff --git a/src/LaravelDebugbar.php b/src/LaravelDebugbar.php index eeba521cf..fd62f9c8a 100644 --- a/src/LaravelDebugbar.php +++ b/src/LaravelDebugbar.php @@ -839,6 +839,7 @@ public function modifyResponse(Request $request, Response $response) && !$this->isJsonRequest($request, $response) && $response->getContent() !== false && in_array($request->getRequestFormat(), [null, 'html'], true) + && !$this->isJsonResponse($response) ) { try { $this->injectDebugbar($response); @@ -912,6 +913,34 @@ protected function isJsonRequest(Request $request, Response $response) return false; } + /** + * @param \Symfony\Component\HttpFoundation\Response $response + * @return bool + */ + protected function isJsonResponse(Response $response) + { + if ($response->headers->get('Content-Type') == 'application/json') { + return true; + } + + $content = $response->getContent(); + + if (is_string($content)) { + if (function_exists('json_validate')) { + return json_validate($content); + } + + // PHP <= 8.2 check + json_decode($content, true); + + return json_last_error() === JSON_ERROR_NONE; + } elseif (is_array($content)) { + return true; + } + + return false; + } + /** * Collects the data from the collectors *