From 540335c778bb6cee4403db90a8c97b3876d2c6e8 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 | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Http/Request.php b/src/Http/Request.php index d97c9375..e62c916e 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -267,6 +267,23 @@ public function getRawBody(): ?string } + /** + * Returns decoded content of HTTP request body. + */ + public function getBody(): mixed + { + $type = $this->getHeader('Content-Type'); + switch ($type) { + case 'application/json': + return json_decode($this->getRawBody()); + case 'application/x-www-form-urlencoded': + return $_POST; + default: + throw new \Exception("Unsupported content type: $type"); + } + } + + /** * Returns basic HTTP authentication credentials. * @return array{string, string}|null