Parse encoded request body data.
Enables JSON and XML request payloads to be parsed into the request's Provides CSRF protection & validation.
You can also add your own request body parsers using the addParser()
method.
__construct(array $options)
Constructor
json
Set to false to disable JSON body parsing.xml
Set to true to enable XML parsing. Defaults to false, as XML handling requires more care than JSON does.methods
The HTTP methods to parse on. Defaults to PUT, POST, PATCH DELETE.array
$options optional The options to use. See above.
addParser(array $types, \Closure $parser)
Add a parser.
Map a set of content-type header values to be parsed by the $parser.
An naive CSV request body parser could be built like so:
$parser->addParser(['text/csv'], function ($body) { return str_getcsv($body); });
string[]
$types An array of content-type header values to match. eg. application/json
\Closure
$parser The parser function. Must return an array of data to be inserted into the request.
$this
decodeJson(string $body)
Decode JSON into an array.
string
$body The request body to decode
array|null
decodeXml(string $body)
Decode XML into an array.
string
$body The request body to decode
array
getMethods()
Get the HTTP methods to parse request bodies on.
string[]
getParsers()
Get the current parsers
\Closure[]
process(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Server\RequestHandlerInterface $handler)
Apply the middleware.
Will modify the request adding a parsed body if the content-type is known.
\Psr\Http\Message\ServerRequestInterface
$request The request.
\Psr\Http\Server\RequestHandlerInterface
$handler The request handler.
\Psr\Http\Message\ResponseInterface
A response.
setMethods(array $methods)
Set the HTTP methods to parse request bodies on.
string[]
$methods The methods to parse data on.
$this
The HTTP methods to parse data on.
string[]
Registered Parsers
\Closure[]
© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.1/class-Cake.Http.Middleware.BodyParserMiddleware.html