Laravel Sanctum 和 Postman 的 POST 请求问题问题、Sanctum、Laravel、POST

2023-09-07 11:10:25 作者:青山不改旧人归

我对与这篇文章相关的 Sanctum 和 Postman 有疑问:

解决方案

我一直在我的一个电子商务 API 中使用 sanctum,并且我也遵循了与您相同的教程在问题中链接.很难说出您的实际问题是什么,但您似乎没有在 POST 请求中发送 X-XSRF-TOKEN 标头.

postman介绍

最后一段在上述教程中,作者展示了如何点击 /logout 路由,这是一个 POST 路由.

I have a problem with Sanctum and Postman that's related to this post: SPA Authentication Issues with Sanctum and Postman

I followed everything from the Laravel docs about Sanctum and configured it correctly. Then I followed this tutorial: https://blog.codecourse.com/laravel-sanctum-airlock-with-postman/

Everything is working except for POST requests. When I do login, it works. But then I have a collection without the pre-request Script and when I do a GET request to for example /user, it will return the logged in user.

But when I change the method to POST in Laravel and in Postman, I'll get a CSRF token mismatch error.

Does anybody knows what I have to do, to make POST requests working?

Route::middleware('auth:sanctum')->post('/user', function (Request $request) {
    return $request->user();
});

解决方案

I've been using sanctum in one of my e-commerce APIs and I've also followed the same tutorial you've linked in the question. It's hard to tell what's the actual problem in your case but it seems like that you're not sending the X-XSRF-TOKEN header in your POST requests.

The last paragraph in the above-mentioned tutorial, the writer shows how to hit the /logout route which is a POST route.