-
Notifications
You must be signed in to change notification settings - Fork 1
ServerRequest: getParsedBody Example
Terry L edited this page Jul 15, 2020
·
5 revisions
Shieldon\Psr7\ServerRequest
Extends Request.
Retrieve any parameters provided in the request body.
-
return
null|array|object
Example:
// Typically, $parsedBody is equal to $_POST superglobal.
$parsedBody = $serverRequest->getParsedBody();Shieldon PSR-HTTP is ready for RESTful, the following content explains how PRS-HTTP deals with the request body.
The getParsedBody method will return:
-
(A) An array of the superglobal $_POST if the request request method is
POSTand the Content-Type is one of the following types:multipart/form-dataapplication/x-www-form-urlencode
-
(B) A JSON object if the request fit to the following conditions.
- The request Content-Type is
application/json - The request body is a valid JSON-formatted string.
- The request method is not
GET.
- The request Content-Type is
-
(C) An array parsed from HTTP build query:
- The condition is neither A or B.
- The request method is not
GET.
-
(D)
nullif the condition is none of above.
| Condition | Method | Content-type | Parsed-body |
|---|---|---|---|
| A | POST | multipart/form-data application/x-www-form-urlencode |
array |
| B | ALL excepts GET | application/json | object |
| C | ALL excepts GET | All excepts A or B | array |
| D | - | - | null |
Hope this helps.
composer require shieldon/psr-httpShieldon PSR HTTP implementation written by Terry L. from Taiwan.
