Parse encoded request body data.
Enables JSON and XML request payloads to be parsed into the request's body. You can also add your own request body parsers using the addParser() method.
Constructor
Add a parser.
Decode JSON into an array.
Decode XML into an array.
Get the HTTP methods to parse request bodies on.
Get the current parsers
Apply the middleware.
Set the HTTP methods to parse request bodies on.
__construct(array<string, mixed> $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<string, mixed> $options optional The options to use. See above.
addParser(array<string> $types, Closure $parser): $this
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);
}); array<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.
$thisdecodeJson(string $body): array|null
Decode JSON into an array.
string $body The request body to decode
array|nulldecodeXml(string $body): array
Decode XML into an array.
string $body The request body to decode
arraygetMethods(): array<string>
Get the HTTP methods to parse request bodies on.
array<string>getParsers(): arrayClosure>
Get the current parsers
arrayClosure>process(ServerRequestInterface $request, RequestHandlerInterface $handler): Psr\Http\Message\ResponseInterface
Apply the middleware.
Will modify the request adding a parsed body if the content-type is known.
ServerRequestInterface $request The request.
RequestHandlerInterface $handler The request handler.
Psr\Http\Message\ResponseInterfacesetMethods(array<string> $methods): $this
Set the HTTP methods to parse request bodies on.
array<string> $methods The methods to parse data on.
$thisThe HTTP methods to parse data on.
array<string>Registered Parsers
arrayClosure>
© 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.4/class-Cake.Http.Middleware.BodyParserMiddleware.html