JsonParser
API reference › yii › web › JsonParser
Parses a raw HTTP request using Json::decode().
To enable parsing for JSON requests you can configure Request::$parsers using this class:
'request' => [ 'parsers' => [ 'application/json' => 'yii\web\JsonParser', ]]Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$asArray | bool | Whether to return objects in terms of associative arrays. Default: true. | JsonParser |
$throwException | bool | Whether to throw a BadRequestHttpException if the body is invalid JSON Default: true. | JsonParser |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
parse() | Parses a HTTP request body. | JsonParser |
Method details
Section titled “Method details”parse()
Section titled “parse() ”public function parse(string $rawBody, string $contentType): mixedParses a HTTP request body.
Parameters
| Parameter | Type | Description |
|---|---|---|
$rawBody | string | The raw HTTP request body. |
$contentType | string | The content type specified for the request body. |
Returns
mixed: Parameters parsed from the request body
Throws
BadRequestHttpException: if the body contains invalid json and$throwExceptionistrue.