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...
Had similar issue. If you are using a form to send put request. Add this hidden field to your form. Also don't forget to move $app-run(); to the last line.
<input type="hidden" name="_METHOD" value="PUT"/>
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