From a20fb8f06a2fa2be4af7bbb7f0943821c943cc50 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sun, 29 Jan 2023 04:28:57 +0100 Subject: [PATCH] Request::getBody() WIP --- src/Http/Request.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Http/Request.php b/src/Http/Request.php index f0511b77..5c7ac021 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -267,6 +267,20 @@ public function getRawBody(): ?string } + /** + * Returns decoded content of HTTP request body. + */ + public function getBody(): mixed + { + $type = $this->getHeader('Content-Type'); + return match ($type) { + 'application/json' => json_decode($this->getRawBody()), + 'application/x-www-form-urlencoded' => $_POST, + default => throw new \Exception("Unsupported content type: $type"), + }; + } + + /** * Returns basic HTTP authentication credentials. * @return array{string, string}|null