Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PUT request body NULL on Slim

I think I have read every answer out there regarding problems getting PUT request body on Slim framework (running on Windows XAMPP). None of the solution seem to work for me, so maybe there is another caveat I'm missing.

My php code follows:

require 'Slim/Slim.php';

\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->run();

$app->put('/calendar/update/:id', function($id) use ($app){
    $ev = json_decode($app->request()->getBody());
    echo var_dump($ev);  // NULL
}

I have been testing it in REST tester in PHPStorm, both using parameters and query string - to no avail. I tried to set contentType: application/x-www-form-urlencoded explicitly in my ajax call as well.

Seem to be getting lost here...

like image 865
user776686 Avatar asked Apr 14 '26 08:04

user776686


1 Answers

The $app->contentType() method only affects the HTTP response sent from Slim back to the HTTP client. I believe Mika was suggesting that you change your HTTP request's Content-Type header to application/json if you intend to parse the request body as shown in your example above.

Also, you should only invoke $app->run() AFTER you define your routes. Your example above invokes $app->run() before your routes.

Hope this helps!

-Josh

like image 137
Josh Lockhart Avatar answered Apr 16 '26 00:04

Josh Lockhart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!