【问题标题】:POST request from Postman to Laravel从 Postman 到 Laravel 的 POST 请求
【发布时间】:2018-02-11 05:39:06
【问题描述】:

我正在尝试使用 Postman 向 Laravel 项目发送发布请求,但收到“419 未知状态”响应

路由\web.php:

Route::post('/myaction', 'MymodelController@myaction');

app\Http\Controllers\MymodelController.php:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Mymodel;

class MymodelController extends Controller
{
    function myaction()
    {
        return redirect('/');
   }
}

为什么会这样?

同样的错误出现独立于myaction()的内容

【问题讨论】:

  • csrf?不,如何通过 Postman 发送?
  • 谢谢!解决方案是使用 api.php 而不是 web.php。 (请将其写为回复,以帮助其他观众)

标签: php laravel post controller postman


【解决方案1】:

你是否定义了redirect('/')的路由;在 web.php 中?

【讨论】:

  • 是的 Route::get('/', function () { return view('welcome'); }); 和 / 中的 GET 请求正常工作。如果我尝试创建Route::post('/',则会出现相同的 419 问题
  • 在您的表单中,您还必须发送 csrf 令牌 {{ csrf_field() }}
【解决方案2】:

当您请求 api 时,您应该在 api.php 而不是 web.php 中写下您的路线

web.php 需要 _token csrf 字段

【讨论】:

    【解决方案3】:

    默认情况下,Laravel 使用中间件VerifyCsrfToken。 见this 了解更多详情。

    您需要将您的 URL 添加到 VerifyCsrfToken 类中的 $excludes 字段中。

    【讨论】:

      猜你喜欢
      • 2021-08-25
      • 2015-09-16
      • 2021-01-09
      • 2021-12-31
      • 1970-01-01
      • 2019-11-12
      • 2020-01-11
      • 1970-01-01
      • 2019-10-11
      相关资源
      最近更新 更多