for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace App\EventSubscriber;
use function json_decode;
use function json_last_error;
use function json_last_error_msg;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class RequestBodySubscriber implements EventSubscriberInterface
{
public function onKernelRequest(GetResponseEvent $event): void
$request = $event->getRequest();
if ('json' !== $request->getContentType() || !$request->getContent()) {
return;
}
$data = json_decode((string) $request->getContent(), true);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new BadRequestHttpException('invalid json body: '.json_last_error_msg());
$request->request->replace(is_array($data) ? $data : []);
public static function getSubscribedEvents(): array
return [
'kernel.request' => 'onKernelRequest',
];