简体   繁体   English

Laravel 419 错误通过 POSTMAN POST 请求

[英]Laravel 419 error on POST request via POSTMAN

I'm trying to send a POST request via the POSTMAN application to my API which is running Laravel 5.6.我正在尝试通过 POSTMAN 应用程序向运行 Laravel 5.6 的 API 发送 POST 请求。

My route is as follows:我的路线如下:

Route::post('/charge','Charge@index');

and the Charge and index function simply var_dumps the post parameter:并且 Charge 和 index 函数只是 var_dumps post 参数:

class Charge extends Controller
{
    public function index()
    {
        var_dump($_POST);
    }

}

The response I get is a 419 unknown status error.我得到的响应是 419 未知状态错误。 I've got no idea what the problem is.我不知道问题是什么。

I'm unsure what other info to include here, but please ask if anything else would be needed to help solve this issue.我不确定此处要包含哪些其他信息,但请询问是否需要其他任何信息来帮助解决此问题。

Thanks, J谢谢,J

It may be because you are not sending your csrf token with the form data.这可能是因为您没有将 csrf 令牌与表单数据一起发送。

In laravel it is mandatory to send the csrf token on every request.在 laravel 中,每次请求都必须发送 csrf 令牌。

If you don't want to send then mention you method name in the app/http/middleware/VerifyCsrfToken.php file.如果您不想发送,请在app/http/middleware/VerifyCsrfToken.php文件中提及您的方法名称。

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;

class VerifyCsrfToken extends Middleware
{

    protected $addHttpCookie = true;

   protected $except = [
    'auth/facebook/callback',
    'auth/google/callback',
];
}

if using postman on headers add如果在标题上使用邮递员添加

(KEY) (VALUE) (核心价值)
X-CSRF-TOKEN yvthwsztyeQkAPzeQ5gHgTvlyxHfsAfE X-CSRF-TOKEN yvthwsztyeQkAPzeQ5gHgTvlyxHfsAfE

you can found VALUE by add您可以通过添加找到 VALUE

public function index()
{
    return csrf_token(); 
}

and send GET on your route name then you will get VALUE of csrf并在您的路线名称上发送 GET 然后您将获得 csrf 的价值

I was having the same problem and the only solution that I found was removing that exact url from the csrf verification file, which name is VerifyCsrfToken.php and is located at我遇到了同样的问题,我发现的唯一解决方案是从 csrf 验证文件中删除那个确切的 url,该文件名为VerifyCsrfToken.php ,位于

app\\Http\\Middleware\\VerifyCsrfToken.php

Once you open that file, you only have to put the exact url that you are doing your post request in the except variable like below:打开该文件后,您只需将您正在执行发布请求的确切 url 放入except变量中,如下所示:

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;

class VerifyCsrfToken extends Middleware
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        //
        'http://localhost/api2/public/user' //This is the url that I dont want Csrf for postman.
    ];
}

After that I could do my post request from postman.之后,我可以从邮递员那里完成我的邮寄请求。

PD: This is for development environments I suppose that you eventually will have to undo this, so, someone correct me if I'm wrong. PD:这是针对开发环境的,我想您最终将不得不撤消此操作,因此,如果我错了,有人会纠正我。

I was making a get request from POSTMAN and facing 419 error.我正在从 POSTMAN 发出获取请求并面临 419 错误。 However, In-case if you are still wondering how to find csrf token even when you are making a GET request and facing status 419 .但是,万一您在发出GET请求并面临状态419时仍然想知道如何找到csrf令牌。 In my case I solved the problem by adding the user-agent: xxxx token in header.就我而言,我通过在标题中添加user-agent: xxxx令牌解决了这个问题。

Example:例子:

user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36

you need to provide CSRF token with the request you send in that case you need a CSRF token.你需要在你发送的请求中提供 CSRF 令牌,在这种情况下你需要一个 CSRF 令牌。

Generating CSRF token on web.php在 web.php 上生成 CSRF 令牌

    Route::get('/token', function () {
        return csrf_token(); 
    });

Sending a request with token |使用令牌发送请求 | PUT FOLLOWING ON HEADERS |token should be change on each request PUT FOLLOWING ON HEADERS |每个请求都应该更改令牌

(KEY)           (VALUE)
X-CSRF-TOKEN    MGpzhSLdVWdB7ddQSR8B6iu3A89A6LW7UPT0zmO2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM