Skip to content

Commit 540335c

Browse files
committed
Request::getBody() WIP
1 parent 7d757a9 commit 540335c

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/Http/Request.php

+17
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,23 @@ public function getRawBody(): ?string
267267
}
268268

269269

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

0 commit comments

Comments
 (0)