diff --git a/Slim/Http/Request.php b/Slim/Http/Request.php index d6c77f9a2..67585d61d 100644 --- a/Slim/Http/Request.php +++ b/Slim/Http/Request.php @@ -1024,6 +1024,20 @@ public function withParsedBody($data) return $clone; } + /** + * Force Body to be parsed again. + * + * Note: This method is not part of the PSR-7 standard. + * + * @return self + */ + public function reparseBody() + { + $this->bodyParsed = false; + + return $this; + } + /** * Register media type parser. * diff --git a/tests/Http/RequestTest.php b/tests/Http/RequestTest.php index ec27eb2d3..82c250caf 100644 --- a/tests/Http/RequestTest.php +++ b/tests/Http/RequestTest.php @@ -772,6 +772,30 @@ public function testGetParsedBodyWhenBodyDoesNotExist() $this->assertNull($request->getParsedBody()); } + public function testGetParsedBodyAfterCallReparseBody() + { + $uri = Uri::createFromString('https://example.com:443/?one=1'); + $headers = new Headers([ + 'Content-Type' => 'application/x-www-form-urlencoded;charset=utf8', + ]); + $cookies = []; + $serverParams = []; + $body = new RequestBody(); + $body->write('foo=bar'); + $body->rewind(); + $request = new Request('POST', $uri, $headers, $cookies, $serverParams, $body); + + $this->assertEquals(['foo' => 'bar'], $request->getParsedBody()); + + $newBody = new RequestBody(); + $newBody->write('abc=123'); + $newBody->rewind(); + $request = $request->withBody($newBody); + $request->reparseBody(); + + $this->assertEquals(['abc' => '123'], $request->getParsedBody()); + } + /** * @expectedException \RuntimeException */