Skip to content

Commit a3719ca

Browse files
committed
Request::getDecodedBody() WIP
1 parent 11b260d commit a3719ca

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/Http/Request.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,20 @@ public function getRawBody(): ?string
266266
}
267267

268268

269+
/**
270+
* Returns decoded content of HTTP request body.
271+
*/
272+
public function getDecodedBody(): mixed
273+
{
274+
$type = $this->getHeader('Content-Type');
275+
return match ($type) {
276+
'application/json' => json_decode($this->getRawBody()),
277+
'application/x-www-form-urlencoded' => $_POST,
278+
default => throw new \Exception("Unsupported content type: $type"),
279+
};
280+
}
281+
282+
269283
/**
270284
* Returns basic HTTP authentication credentials.
271285
* @return array{string, string}|null

0 commit comments

Comments
 (0)