How can I try sending a post request to a Laravel app with Postman?

Normally Laravel has a csrf_token that we have to pass with a POST/PUT request. How can I get and send this value in Postman? Is it even possible without turning off the CSRF protection?

推荐答案

Edit:

Ah wait, I misread the question. You want to do it without turning off the CSRF protection? Like Bharat Geleda said: You can make a route that returns only the token and manually copy it in a _token field in postman.

But I would recommend excluding your api calls from the CSRF protection like below, and addin some sort of API authentication later.

Which version of laravel are you running?

Laravel 5.2及以上版本:

从5.2开始,CSRF令牌仅在具有web个中间件的路由上需要.因此,将您的API路由放在具有web个中间件的组之外.

See the "The Default Routes File" heading in the documentation for more info.

Laravel 5.1和5.2:

You can exclude routes which should not have CSRF protection in the VerifyCsrfToken middleware like this:

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        'api/*',
    ];
}

有关更多信息,请参阅标题documentation"从CSRF保护中排除URI".

Laravel相关问答推荐

AJAX响应中未定义的全名

在GitHub操作中缺少PHPStan和PHPMD输出,但在本地存在

Vue 组件中的图像不适用于 Vite + Laravel

如何在自定义表单请求中获取错误验证数据

从主类别模型 laravel 中获取子类别翻译

无法在 Laravel 项目中安装 @vitejs/plugin-vue

laravel 如何验证跨越午夜的两个值之间的时间(15:00 > 时间 > 01:00)

Laravel 从 5.1 升级到 5.2.0 错误

laravel 基本路径

使用 Laravel 和 Passport 验证失败时响应状态码 401?

路由中间的可选参数

Laravel Artisan Scheduler 中的链式命令?

Laravel 表 * 没有名为 * 的列

Laravel 5 - Php artisan语法错误

删除表后如何重新迁移laravel迁移

如何在 Laravel 5 中验证当前、新密码和新密码确认?

如何在 Laravel 中实现数组类型路由?

如何调试 Laravel 框架?

扩展 Eloquent 的类的构造函数

Laravel:Redis 无法建立连接:[tcp://127.0.0.1:6379]