It’s very common in web APIs to send data in JSON or XML format.
Out of the box, PSR-7 implementations do not support these formats, you have to decode the Request object’s getBody() yourself.
As this is a common requirement, Slim 4 provides BodyParsingMiddleware
to handle this task.
It’s recommended to put the body parsing middleware before the call to addErrorMiddlware
, so that the stack looks like this:
<?php
use Slim\Factory\AppFactory;
require_once __DIR__ . '/../vendor/autoload.php';
$app = AppFactory::create();
// Parse json, form data and xml
$app->addBodyParsingMiddleware();
$app->addRoutingMiddleware();
$app->addErrorMiddleware(true, true, true);
// ...
$app->run();
No changes are required to the POST handler because the BodyParsingMiddleware
detects that the Content-Type
is set to a JSON
media type and so places the decoded body into the Request’s parsed body property.
For data posted to the website from a browser, you can use the $request’s getParsedBody()
method.
This will return an array of the posted data.
$app->post('/', function (Request $request, Response $response, $args): Response {
$data = $request->getParsedBody();
$html = var_export($data, true);
$response->getBody()->write($html);
return $response;
});
Content-Type
from the request header to detect the media type.application/
.