0

I created route with method POST. When i send request with body, but i trying use

$request->getParsedBody()

I am always takes NULL. Why?

<?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();

Request from Postman

POST /test HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Cache-Control: no-cache

{
    "name": "test",
    "email": "test@mail.ru"
}
odan
  • 4,757
  • 5
  • 20
  • 49

1 Answers1

2

Try to add the Body Parsing Middleware

$app->addBodyParsingMiddleware();
odan
  • 4,757
  • 5
  • 20
  • 49