Skip to content
PHP Ecosystem
Explore

JsonParser

API reference › yii › web › JsonParser

classyii\web\JsonParser
ImplementsRequestParserInterface
Since2.0
Sourceframework/web/JsonParser.php

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',
]
]
PropertyTypeDescriptionDefined by
$asArrayboolWhether to return objects in terms of associative arrays. Default: true.JsonParser
$throwExceptionboolWhether to throw a BadRequestHttpException if the body is invalid JSON Default: true.JsonParser
MethodDescriptionDefined by
parse()Parses a HTTP request body.JsonParser
public function parse(string $rawBody, string $contentType): mixed

Parses a HTTP request body.

Parameters

ParameterTypeDescription
$rawBodystringThe raw HTTP request body.
$contentTypestringThe content type specified for the request body.

Returns

mixed: Parameters parsed from the request body

Throws