Laravel 5 GET api 调用有效,但 POST api 调用无效
Laravel 5 GET api call is working but POST api calls are not working
如何在 laravel 中的 API 路由上禁用 CSRF 验证?
在 API routes
中,我通过 POST 和 GET 方法调用一个函数。 GET 方法返回数据但 POST 方法抛出方法未找到异常。
这是我在 routes/api.php
中的示例代码
Route::post('hellopostapi', function() {
return json_encode( 'we are getting POST response');
});
Route::get('helloget', function() {
return json_encode( 'we are getting GET response);
});
对于 GET 调用,我得到了预期的响应。
对于 POST 我得到了这个例外
"message": "",
"exception":"Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException",
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",
"line": 255,
"trace": [
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",
"line": 242,
"function": "methodNotAllowed",
"class": "Illuminate\Routing\RouteCollection",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",
"line": 176,
"function": "getRouteForMethods",
"class": "Illuminate\Routing\RouteCollection",
"type": "->"
},
我还在 verifycsrf.php 中添加了 api 路由,根据文档它应该可以工作,不幸的是它不适合我。
这是我的 verifiycsrf.php
class VerifyCsrfToken extends BaseVerifier
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
'api/*',
];
}
API 测试截图
POST 导致问题的请求
获取工作正常的请求
那仍然行不通。谁能帮帮我。谢谢
检查您发布的表单方法,该方法很可能是 GET 方法,但在您的路由文件中您已将其配置为期望 POST 方法
深入研究后,我发现这绝不是 laravel 问题,而是由 HTTP 到 HTTPS 重定向引起的。
将 http 更改为 https 后,问题似乎得到解决。 :)
如何在 laravel 中的 API 路由上禁用 CSRF 验证?
在 API routes
中,我通过 POST 和 GET 方法调用一个函数。 GET 方法返回数据但 POST 方法抛出方法未找到异常。
这是我在 routes/api.php
Route::post('hellopostapi', function() {
return json_encode( 'we are getting POST response');
});
Route::get('helloget', function() {
return json_encode( 'we are getting GET response);
});
对于 GET 调用,我得到了预期的响应。 对于 POST 我得到了这个例外
"message": "", "exception":"Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException",
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php", "line": 255, "trace": [ { "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php", "line": 242, "function": "methodNotAllowed", "class": "Illuminate\Routing\RouteCollection", "type": "->" }, { "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php", "line": 176, "function": "getRouteForMethods", "class": "Illuminate\Routing\RouteCollection", "type": "->" },
我还在 verifycsrf.php 中添加了 api 路由,根据文档它应该可以工作,不幸的是它不适合我。
这是我的 verifiycsrf.php
class VerifyCsrfToken extends BaseVerifier
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
'api/*',
];
}
API 测试截图
POST 导致问题的请求
获取工作正常的请求
检查您发布的表单方法,该方法很可能是 GET 方法,但在您的路由文件中您已将其配置为期望 POST 方法
深入研究后,我发现这绝不是 laravel 问题,而是由 HTTP 到 HTTPS 重定向引起的。 将 http 更改为 https 后,问题似乎得到解决。 :)