Skip to content

Commit 76002b1

Browse files
[HttpFoundation] Make Request::getPayload() return an empty InputBag if request body is empty
1 parent 87b8bfb commit 76002b1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Request.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,25 @@ public function getContent(bool $asResource = false)
15131513
*/
15141514
public function getPayload(): InputBag
15151515
{
1516-
return $this->request->count() ? clone $this->request : new InputBag($this->toArray());
1516+
if ($this->request->count()) {
1517+
return clone $this->request;
1518+
}
1519+
1520+
if ('' === $content = $this->getContent()) {
1521+
return new InputBag([]);
1522+
}
1523+
1524+
try {
1525+
$content = json_decode($content, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
1526+
} catch (\JsonException $e) {
1527+
throw new JsonException('Could not decode request body.', $e->getCode(), $e);
1528+
}
1529+
1530+
if (!\is_array($content)) {
1531+
throw new JsonException(sprintf('JSON content was expected to decode to an array, "%s" returned.', get_debug_type($content)));
1532+
}
1533+
1534+
return new InputBag($content);
15171535
}
15181536

15191537
/**

Tests/RequestTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,9 @@ public function testGetPayload()
13091309

13101310
$req = new Request([], ['foo' => 'bar'], [], [], [], [], json_encode(['baz' => 'qux']));
13111311
$this->assertSame(['foo' => 'bar'], $req->getPayload()->all());
1312+
1313+
$req = new Request([], [], [], [], [], [], '');
1314+
$this->assertSame([], $req->getPayload()->all());
13121315
}
13131316

13141317
/**

0 commit comments

Comments
 (0)