From d10f3480c146cfd4eae686ae717c09b91263918e Mon Sep 17 00:00:00 2001 From: fliespl Date: Wed, 10 Mar 2021 09:20:52 +0100 Subject: [PATCH 1/2] POST method should also not throw exception on empty payload without content-type Content-Type header field is used to specify type of data in the body request. When request is sent with an empty payload - it should be accepted the same way as DELETE request, cause in such cases Content-Type header is not applicable. --- EventListener/BodyListener.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/EventListener/BodyListener.php b/EventListener/BodyListener.php index cdf0e80b9..7b1ddbc9c 100644 --- a/EventListener/BodyListener.php +++ b/EventListener/BodyListener.php @@ -76,7 +76,7 @@ public function onKernelRequest(RequestEvent $event): void if (null === $format || !$this->decoderProvider->supports($format)) { if ($this->throwExceptionOnUnsupportedContentType - && $this->isNotAnEmptyDeleteRequestWithNoSetContentType($method, $content, $contentType) + && $this->isNotAnEmptyPostOrDeleteRequestWithNoSetContentType($method, $content, $contentType) ) { throw new UnsupportedMediaTypeHttpException("Request body format '$format' not supported"); } @@ -109,9 +109,9 @@ public function onKernelRequest(RequestEvent $event): void } } - private function isNotAnEmptyDeleteRequestWithNoSetContentType(string $method, $content, ?string $contentType): bool + private function isNotAnEmptyPostOrDeleteRequestWithNoSetContentType(string $method, $content, ?string $contentType): bool { - return false === ('DELETE' === $method && empty($content) && empty($contentType)); + return false === (in_array($method, ['DELETE', 'POST], true) && empty($content) && empty($contentType)); } private function isDecodeable(Request $request): bool From 3456ecd629822455831fbc49f7092dec2b7c274f Mon Sep 17 00:00:00 2001 From: fliespl Date: Wed, 10 Mar 2021 09:24:32 +0100 Subject: [PATCH 2/2] Update BodyListener.php --- EventListener/BodyListener.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EventListener/BodyListener.php b/EventListener/BodyListener.php index 7b1ddbc9c..585f436f7 100644 --- a/EventListener/BodyListener.php +++ b/EventListener/BodyListener.php @@ -111,7 +111,7 @@ public function onKernelRequest(RequestEvent $event): void private function isNotAnEmptyPostOrDeleteRequestWithNoSetContentType(string $method, $content, ?string $contentType): bool { - return false === (in_array($method, ['DELETE', 'POST], true) && empty($content) && empty($contentType)); + return false === (in_array($method, ['DELETE', 'POST'], true) && empty($content) && empty($contentType)); } private function isDecodeable(Request $request): bool