Skip to content

Commit 1b8b368

Browse files
committed
Request::getDecodedBody() WIP
1 parent 343a63a commit 1b8b368

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
@@ -268,6 +268,20 @@ public function getRawBody(): ?string
268268
}
269269

270270

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

0 commit comments

Comments
 (0)