Johnmark
Johnmark

Reputation: 65

I can't receive POST data in php with slim framework

I try to make a RESTful api using slim framework. I do not know details about the slim framework, because i'm beginner of this framework. So, I followed all steps of how to get data in slimframework. But i can't get POST data.


Follow is my code to get POST data.

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

$app->post('/registration/user', function (Request $request, Response $response) use ($app) {
$data = $request->getParsedBody();
$name = $_POST['name'];
echo '1 : '.$data['name'];
echo '2 : '.$name;
});

That is result of when i send name data.

enter image description here

what i have to do for get post data?? Do you have any idea?

Upvotes: 2

Views: 3326

Answers (2)

Ajay Kamble
Ajay Kamble

Reputation: 1

The following can be used:

$body= file_get_contents("php://input"); 
// $body = $request->getBody();
$data = json_decode($body, true);

Upvotes: 0

turbopixel
turbopixel

Reputation: 142

I use \Slim\Http\Request $request->getParsedBody() in my routes. The method contains all the $_POST data.

Example:

$postArr  = $request->getParsedBody();
$name     = $postArr["name"];

Upvotes: 2

Related Questions