Я создал маршрут методом POST. Когда я отправляю запрос с телом, но пытаюсь использовать
$request->getParsedBody()
Я всегда беру NULL. Почему?
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
$app = AppFactory::create();
$app->addErrorMiddleware(true, false, false);
$app->post('/add', function (Request $request, Response $response, array $args) {
$data = $request->getParsedBody();
return $response;
});
$app->run();
Запрос от почтальона
POST /test HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Cache-Control: no-cache
{
"name": "test",
"email": "[email protected]"
}
Попробуйте добавить ПО промежуточного слоя разбора тела
$app->addBodyParsingMiddleware();