Skip to content

Commit 6b2f5df

Browse files
committed
feature #117 Leverage Request::getPayload() to populate the parsed body of PSR-7 requests (AurelienPillevesse)
This PR was squashed before being merged into the 2.2-dev branch. Discussion ---------- Leverage `Request::getPayload()` to populate the parsed body of PSR-7 requests I'm trying to send a ```POST``` request with the fields below and ```application/x-www-form-urlencoded``` header : - "city": "Paris" - "country": "France" If I send it like that, the fields are understood and I have these fields when I display the Request : - [ "city": "Paris", "country": "France" ] Now if I send the same ```POST``` request with ```application/json``` header (with the same fields but in ```JSON``` format) : - {"city":"Paris","country":"France"} They are not understand and I have an empty Request : - [] Commits ------- 3a8caad Leverage `Request::getPayload()` to populate the parsed body of PSR-7 requests
2 parents 18c9e82 + 3a8caad commit 6b2f5df

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

Factory/PsrHttpFactory.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Psr\Http\Message\UploadedFileInterface;
2121
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
2222
use Symfony\Component\HttpFoundation\BinaryFileResponse;
23+
use Symfony\Component\HttpFoundation\Exception\JsonException;
2324
use Symfony\Component\HttpFoundation\File\UploadedFile;
2425
use Symfony\Component\HttpFoundation\Request;
2526
use Symfony\Component\HttpFoundation\Response;
@@ -29,6 +30,7 @@
2930
* Builds Psr\HttpMessage instances using a PSR-17 implementation.
3031
*
3132
* @author Antonio J. García Lagar <aj@garcialagar.es>
33+
* @author Aurélien Pillevesse <aurelienpillevesse@hotmail.fr>
3234
*/
3335
class PsrHttpFactory implements HttpMessageFactoryInterface
3436
{
@@ -71,12 +73,28 @@ public function createRequest(Request $symfonyRequest)
7173

7274
$body = $this->streamFactory->createStreamFromResource($symfonyRequest->getContent(true));
7375

76+
if (method_exists(Request::class, 'getContentTypeFormat')) {
77+
$format = $symfonyRequest->getContentTypeFormat();
78+
} else {
79+
$format = $symfonyRequest->getContentType();
80+
}
81+
82+
if (method_exists(Request::class, 'getPayload') && 'json' === $format) {
83+
try {
84+
$parsedBody = $symfonyRequest->getPayload()->all();
85+
} catch (JsonException $e) {
86+
$parsedBody = [];
87+
}
88+
} else {
89+
$parsedBody = $symfonyRequest->request->all();
90+
}
91+
7492
$request = $request
7593
->withBody($body)
7694
->withUploadedFiles($this->getFiles($symfonyRequest->files->all()))
7795
->withCookieParams($symfonyRequest->cookies->all())
7896
->withQueryParams($symfonyRequest->query->all())
79-
->withParsedBody($symfonyRequest->request->all())
97+
->withParsedBody($parsedBody)
8098
;
8199

82100
foreach ($symfonyRequest->attributes->all() as $key => $value) {

Tests/Factory/PsrHttpFactoryTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
/**
2626
* @author Kévin Dunglas <dunglas@gmail.com>
2727
* @author Antonio J. García Lagar <aj@garcialagar.es>
28+
* @author Aurélien Pillevesse <aurelienpillevesse@hotmail.fr>
2829
*/
2930
class PsrHttpFactoryTest extends TestCase
3031
{
@@ -246,4 +247,52 @@ public function testUploadErrNoFile()
246247
$this->assertSame(\UPLOAD_ERR_NO_FILE, $uploadedFiles['f1']->getError());
247248
$this->assertSame(\UPLOAD_ERR_NO_FILE, $uploadedFiles['f2']->getError());
248249
}
250+
251+
public function testJsonContent()
252+
{
253+
if (!method_exists(Request::class, 'getPayload')) {
254+
$this->markTestSkipped();
255+
}
256+
257+
$headers = [
258+
'HTTP_HOST' => 'http_host.fr',
259+
'CONTENT_TYPE' => 'application/json',
260+
];
261+
$request = new Request([], [], [], [], [], $headers, '{"city":"Paris","country":"France"}');
262+
$psrRequest = $this->factory->createRequest($request);
263+
264+
$this->assertSame(['city' => 'Paris', 'country' => 'France'], $psrRequest->getParsedBody());
265+
}
266+
267+
public function testEmptyJsonContent()
268+
{
269+
if (!method_exists(Request::class, 'getPayload')) {
270+
$this->markTestSkipped();
271+
}
272+
273+
$headers = [
274+
'HTTP_HOST' => 'http_host.fr',
275+
'CONTENT_TYPE' => 'application/json',
276+
];
277+
$request = new Request([], [], [], [], [], $headers, '{}');
278+
$psrRequest = $this->factory->createRequest($request);
279+
280+
$this->assertSame([], $psrRequest->getParsedBody());
281+
}
282+
283+
public function testWrongJsonContent()
284+
{
285+
if (!method_exists(Request::class, 'getPayload')) {
286+
$this->markTestSkipped();
287+
}
288+
289+
$headers = [
290+
'HTTP_HOST' => 'http_host.fr',
291+
'CONTENT_TYPE' => 'application/json',
292+
];
293+
$request = new Request([], [], [], [], [], $headers, '{"city":"Paris"');
294+
$psrRequest = $this->factory->createRequest($request);
295+
296+
$this->assertSame([], $psrRequest->getParsedBody());
297+
}
249298
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy