Slim,Postman和AngularJs:$ app-> request-> getBody()vs $ app-> request-> post()

wwr*_*wwr 1 rest slim angularjs postman

我是初学者.我在客户端编写了一个由AngularJs GUI构成的测试应用程序,在服务器端编写了一个PHP API.

这是处理请求的角度服务

myApp.factory('Book', ['$resource', 'API_URL', function($resource, API_URL){

    return $resource(API_URL + '/books/:bookId', {bookId: '@bookId'}, {
        get: { method: 'GET', isArray:true },
        update: { method: 'PUT'},
        save: { method: 'POST'},
        delete: {method:'DELETE'},
    });

}]);
Run Code Online (Sandbox Code Playgroud)

当我从Angular应用程序提交一本书时,我可以通过使用来捕获Slim中的POST

$post_a = json_decode($app->request->getBody());
//$post_b = $app->request->post(); //this would be empty
Run Code Online (Sandbox Code Playgroud)

当我使用Postman并执行POST时,我可以通过使用来捕获Slim中的POST

//$post_a = json_decode($app->request->getBody()); // this would be empty
$post_b = $app->request->post();
Run Code Online (Sandbox Code Playgroud)

我不明白为什么会有这种差异.你能解释一下吗?

我不打算只用$ app-> request-> post(); 在这两种情况下?为什么来自Angular的帖子只能用$ app-> request-> getBody()来捕获?

小智 5

$app->request->post()方法检索application/x-www-form-urlencoded请求中提交的键/值数据.如果请求使用不同的内容类型(例如application/json),则可以使用该$app->request->getBody()方法检索原始请求主体并根据需要对其进行解码.如果您有其他问题,请与我们联系.