从邮递员到 Laravel 的 POST 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46012380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:35:25  来源:igfitidea点击:

POST request from Postman to Laravel

phplaravelpostcontrollerpostman

提问by Irini Koutaki

I am trying to send a post request to a Laravel project using Postman, but I get a "419 unknown status" response

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

routes\web.php:

路线\ web.php:

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

app\Http\Controllers\MymodelController.php:

应用\Http\Controllers\MymodelController.php:

<?php
namespace App\Http\Controllers;

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

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

Why does this happen?

为什么会发生这种情况?

The same error appears independently of the content of myaction()

出现相同的错误与内容无关 myaction()

回答by Niklesh Raut

As you are requesting api you should write your route in api.phpinstead web.php

正如您所要求的API,你应该写你的路线api.php,而不是web.php

web.phprequire _tokenthe csrf field

web.php需要_tokencsrf 字段

回答by Antoine

By default, Laravel use the middleware VerifyCsrfToken. See thisfor more details.

默认情况下,Laravel 使用中间件VerifyCsrfToken。有关 更多详细信息,请参阅内容。

You need to add your URL to the $excludesfield inside VerifyCsrfTokenclass.

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

回答by Nebojsa Zlatanovic

Do you have defined route for redirect('/'); in web.php ?

你有没有为redirect('/')定义路由?在 web.php 中?