Request body always empty

I am faced with a weird issue where the request body (both parsed and raw string body) is always empty with slim php. I removed all middleware from my code, re-installed composer packages and went as far to just run the hello world example modified as POST request like this:

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->addRoutingMiddleware();
$errorMiddleware = $app->addErrorMiddleware(true, true, true);

$app->post('/', function (Request $request, Response $response, $args) {
    var_dump(file_get_contents('php://input'));
    var_dump((string) $request->getBody()));
    $response->getBody()->write("Hello");
    return $response;
});

$app->run();

In every case the body is empty, I sent raw text, form data and JSON but it does not change. The php input however does have the correct body, just slim seems to be unable to get it at all.

Maybe you are looking for $request->getParsedBody() in Slim 4.

I added this helper into my project, if it helps:

/**
  * Fetch parameter value from request body.
  *
  * @param      $request
  * @param      $key
  * @param null $default
  *
  * @return null
  */
public static function getParsedBodyParam(Request $request, string $key, $default = null) {
  $postParams = $request->getParsedBody();
  $result = $default;
  if (is_array($postParams) && isset($postParams[$key])) {
      $result = $postParams[$key];
  } elseif (is_object($postParams) && property_exists($postParams, $key)) {
      $result = $postParams->$key;
  }
  return $result;
}

Thanks for your suggestion! Unfortunately the parsed body also returned null.

I did track this down to an outdated version of slim/psr7, that was stuck at version 0.3. Once updated to the latest version everything worked fine.

Bad luck that this was caused by something that caused no error and wasn’t due to a specific middleware, which seems to be a common cause.