Skip to content

ServerRequest: getParsedBody Example

Terry L edited this page Jul 15, 2020 · 5 revisions
Clone this wiki locally

Shieldon\Psr7\ServerRequest

Extends Request.

getParsedBody()

Retrieve any parameters provided in the request body.

  • return null|array|object

Example:

// Typically, $parsedBody is equal to $_POST superglobal.
$parsedBody = $serverRequest->getParsedBody();

The Behavior of Handling Request Body

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 POST and the Content-Type is one of the following types:

    • multipart/form-data
    • application/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.
  • (C) An array parsed from HTTP build query:

    • The condition is neither A or B.
    • The request method is not GET.
  • (D) null if the condition is none of above.

Summary

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.