diff --git a/src/Illuminate/Http/Request.php b/src/Illuminate/Http/Request.php index f80f74700dc..40a61eff573 100644 --- a/src/Illuminate/Http/Request.php +++ b/src/Illuminate/Http/Request.php @@ -405,7 +405,7 @@ public function get(string $key, mixed $default = null): mixed public function json($key = null, $default = null) { if (! isset($this->json)) { - $this->json = new InputBag((array) json_decode($this->getContent(), true)); + $this->json = new InputBag((array) json_decode($this->getContent() ?: '[]', true)); } if (is_null($key)) { diff --git a/tests/Http/HttpRequestTest.php b/tests/Http/HttpRequestTest.php index 9c6a5246343..7c4f26872d5 100644 --- a/tests/Http/HttpRequestTest.php +++ b/tests/Http/HttpRequestTest.php @@ -1606,4 +1606,14 @@ public function testItCanHaveObjectsInJsonPayload() $this->assertSame(['name' => 'Laravel'], $request->get('framework')); } + + public function testItDoesNotGenerateJsonErrorsForEmptyContent() + { + // clear any existing errors + json_encode(null); + + Request::create('', 'GET')->json(); + + $this->assertTrue(json_last_error() === JSON_ERROR_NONE); + } }