我使用Postman(在Chrome中)测试Slim调用,但无法弄清楚如何获取任何发布的JSON数据.
我提交原始JSON:
{"name":"John Smith", "age":"30", "gender":"male"}
使用内容类型:标题中的application/json
通过POST到:
http://domain/api/v1/users/post/test/
每次获取JSON数据的尝试都会产生致命错误(请参阅下面的代码注释)
add(new \Slim\Middleware\ContentTypes()); $app->group('/api/v1', function () use ($app) { $app->group('/users/post', function () use ($app) { $app->post('/test/', function () { print_r($app->request->headers); //no errors, but no output? echo "Hello!"; // outputs just fine $data = $app->request()->params('name'); //Fatal error: Call to a member function request() on a non-object $data = $app->request->getBody(); //Fatal error: Call to a member function getBody() on a non-object $data = $app->request->post('name'); //Fatal error: Call to a member function post() on a non-object $data = $app->request()->post(); //Fatal error: Call to a member function request() on a non-object print_r($data); echo $data; }); }); }); $app->run(); ?>
我错过了什么?
谢谢!
确保咖喱$app
进入最后一个嵌套路线,如下所示:
// Pass through $app $app->post('/test/', function () use ($app) {
你在其他任何地方都这样做,所以我假设你只是忽略了它.