Skip to content

Commit

Permalink
Handle string based payloads (#49047)
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald authored Nov 20, 2023
1 parent e5f0354 commit 7af8733
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Illuminate/Routing/RoutingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,15 @@ protected function registerPsrRequest()
$psr17Factory = new Psr17Factory;

return with((new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory))
->createRequest($illuminateRequest = $app->make('request')), fn ($request) => $request->withParsedBody(
array_merge($request->getParsedBody() ?? [], $illuminateRequest->getPayload()->all())
));
->createRequest($illuminateRequest = $app->make('request')), function (ServerRequestInterface $request) use ($illuminateRequest) {
if ($illuminateRequest->getContentTypeFormat() !== 'json' && $illuminateRequest->request->count() === 0) {
return $request;
}

return $request->withParsedBody(
array_merge($request->getParsedBody() ?? [], $illuminateRequest->getPayload()->all())
);
});
}

throw new BindingResolutionException('Unable to resolve PSR request. Please install the symfony/psr-http-message-bridge and nyholm/psr7 packages.');
Expand Down
Binary file not shown.
12 changes: 12 additions & 0 deletions tests/Integration/Foundation/RoutingServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ public function testItIncludesMergedDataInServerRequestInterfaceInstancesUsingPo
'request-data' => 'request-data',
]);
}

public function testItHandlesGzippedBodyPayloadsWhenCreatingServerRequestInterfaceInstances()
{
Route::post('test-route', function (ServerRequestInterface $request) {
return gzdecode((string) $request->getBody());
});

$response = $this->call('POST', 'test-route', content: file_get_contents(__DIR__.'/Fixtures/laravel.txt.gz'));

$response->assertOk();
$response->assertContent("Laravel\n");
}
}

class MergeDataMiddleware
Expand Down

0 comments on commit 7af8733

Please sign in to comment.