Abel Jojo
Abel Jojo

Reputation: 758

Slim 3 post method returns empty

When I submit a from action to this url, and also test this api via Postman. It is not printing POST data.

But get method is working.

$app = new \Slim\App;

$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();

$app->add(new \Slim\Middleware\JwtAuthentication([
    //"secure"      => false,
    //"relaxed"         => ["localhost", "api.f2f.dev"],
    "header"        => "X-Token",
    "path"          => ["/v2"],
    "passthrough"   => ["/v1/api/token/", "/test", "/v1"],
    "secret"        => getenv("TOKEN_SECRET")
]));

$app->post("/v1/app/register", function ($request, $response, $arguments) {
     return $allPostPutVars = $request->getParsedBody();
});

I couldn't find the issue i this. But unparsed data is able to print.

Any help is welcome. Any post method on Slim 3 is also welcomed.

Thank you.

Upvotes: 0

Views: 2641

Answers (1)

Georgy Ivanov
Georgy Ivanov

Reputation: 1579

Your callback should return response object that implements Psr\Http\Message\ResponseInterface. It doesn't. So, as an example:

$app->post("/v1/app/register", function ($request, $response, $arguments) {
    $params = $request->getParams();
    return $response->getBody()->write('You have posted '.count($params).' parameters.');
});

Sometimes you need a quick and dirty check. Then you can do the following:

$app->post("/v1/app/register", function ($request, $response, $arguments) {
    $params = $request->getParams();
    print_r($params);
    die();
});

Upvotes: 2

Related Questions