CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 5.0 Chiffon API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 5.0
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • Cache
    • Collection
    • Command
    • Console
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Form
    • Http
      • Client
      • Cookie
      • Exception
      • Middleware
      • Session
      • TestSuite
    • I18n
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View

Class BodyParserMiddleware

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.

Namespace: Cake\Http\Middleware

Property Summary

  • $methods protected
    list<string>

    The HTTP methods to parse data on.

  • $parsers protected
    array<Closure>

    Registered Parsers

Method Summary

  • __construct() public

    Constructor

  • addParser() public

    Add a parser.

  • decodeJson() protected

    Decode JSON into an array.

  • decodeXml() protected

    Decode XML into an array.

  • getMethods() public

    Get the HTTP methods to parse request bodies on.

  • getParsers() public

    Get the current parsers

  • process() public

    Apply the middleware.

  • setMethods() public

    Set the HTTP methods to parse request bodies on.

Method Detail

__construct() ¶ public

__construct(array<string, mixed> $options = [])

Constructor

Options

  • 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.
Parameters
array<string, mixed> $options optional

The options to use. See above.

addParser() ¶ public

addParser(list<string> $types, Closure $parser): $this

Add a parser.

Map a set of content-type header values to be parsed by the $parser.

Example

An naive CSV request body parser could be built like so:

$parser->addParser(['text/csv'], function ($body) {
  return str_getcsv($body);
});
Parameters
list<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.

Returns
$this

decodeJson() ¶ protected

decodeJson(string $body): array|null

Decode JSON into an array.

Parameters
string $body

The request body to decode

Returns
array|null

decodeXml() ¶ protected

decodeXml(string $body): array

Decode XML into an array.

Parameters
string $body

The request body to decode

Returns
array

getMethods() ¶ public

getMethods(): array<string>

Get the HTTP methods to parse request bodies on.

Returns
array<string>

getParsers() ¶ public

getParsers(): array<Closure>

Get the current parsers

Returns
array<Closure>

process() ¶ public

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.

Parameters
ServerRequestInterface $request

The request.

RequestHandlerInterface $handler

The request handler.

Returns
Psr\Http\Message\ResponseInterface

setMethods() ¶ public

setMethods(list<string> $methods): $this

Set the HTTP methods to parse request bodies on.

Parameters
list<string> $methods

The methods to parse data on.

Returns
$this

Property Detail

$methods ¶ protected

The HTTP methods to parse data on.

Type
list<string>

$parsers ¶ protected

Registered Parsers

Type
array<Closure>
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs