Skip to content

Commit a3ada8b

Browse files
committed
feature #49614 [HttpFoundation] add Request::getPayload() (kbond)
This PR was merged into the 6.3 branch. Discussion ---------- [HttpFoundation] add `Request::getPayload()` | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #47646 | License | MIT | Doc PR | todo Alternative for #47938 based on feedback there. ```php /** `@var` InputBag $input wrapping either Request::$request or Request::toArray() */ $input = $request->getPayload(); ``` Commits ------- 4a0a439933 [HttpFoundation] add `Request::getPayload()`
2 parents 2b6fdfd + 622df58 commit a3ada8b

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ CHANGELOG
1010
* Create migration for session table when pdo handler is used
1111
* Add support for Relay PHP extension for Redis
1212
* The `Response::sendHeaders()` method now takes an optional HTTP status code as parameter, allowing to send informational responses such as Early Hints responses (103 status code)
13-
* Add `IpUtils::isPrivateIp`
13+
* Add `IpUtils::isPrivateIp()`
14+
* Add `Request::getPayload(): InputBag`
1415
* Deprecate conversion of invalid values in `ParameterBag::getInt()` and `ParameterBag::getBoolean()`,
1516
* Deprecate ignoring invalid values when using `ParameterBag::filter()`, unless flag `FILTER_NULL_ON_FAILURE` is set
1617

Request.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,14 @@ public function getContent(bool $asResource = false)
15041504
return $this->content;
15051505
}
15061506

1507+
/**
1508+
* Gets the decoded form or json request body.
1509+
*/
1510+
public function getPayload(): InputBag
1511+
{
1512+
return $this->request->count() ? clone $this->request : new InputBag($this->toArray());
1513+
}
1514+
15071515
/**
15081516
* Gets the request body decoded as array, typically from a JSON payload.
15091517
*

Tests/RequestTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,17 @@ public function testToArray()
13001300
$this->assertEquals(['foo' => 'bar'], $req->toArray());
13011301
}
13021302

1303+
public function testGetPayload()
1304+
{
1305+
$req = new Request([], [], [], [], [], [], json_encode(['foo' => 'bar']));
1306+
$this->assertSame(['foo' => 'bar'], $req->getPayload()->all());
1307+
$req->getPayload()->set('new', 'key');
1308+
$this->assertSame(['foo' => 'bar'], $req->getPayload()->all());
1309+
1310+
$req = new Request([], ['foo' => 'bar'], [], [], [], [], json_encode(['baz' => 'qux']));
1311+
$this->assertSame(['foo' => 'bar'], $req->getPayload()->all());
1312+
}
1313+
13031314
/**
13041315
* @dataProvider provideOverloadedMethods
13051316
*/

0 commit comments

Comments
 (0)