Laravel API 无法在本地主机的邮递员中工作

问题描述 投票:0回答:2

我已经将api开发成php laravel,并在本地服务器上开发并使用它们运行

php artisan serve

这些 api 正在网站中运行,但每当我尝试在邮递员中访问它们时,它都会返回 419 响应代码和一条消息 页面已过期

在谷歌上搜索调试来自网站的请求后,我发现了与请求一起传递的标头。 所以我就通过了

_token 
X-CSRF-Token 

并且还在授权选项卡中选择了承载令牌。即使在标头中添加这些参数后也没有成功。我尝试将它们添加到 body 中,但在那里也不起作用。

代码片段是

Route::post('/register_user', 'MyUserController@storeApi');

我的用户控制器

 public function storeApi(Request $request)
 {   
     
        $userModel = new User;
        $userModel->first_name = $request->input('first_name');
        $userModel->last_name = $request->input('last_name');
        $userModel->save();
    
        $userId = DB::getPdo()->lastInsertId();

        $response['status'] = 'ok';
        $response['response'] = $userId ;
        return response()->json($response);
            
 }

所以我的问题是,如何使用邮递员使其在本地服务器中工作。

php laravel postman laravel-artisan artisan-serve
2个回答
3
投票

使用

api.php
文件 并使用 api 前缀调用路由,如下所示:

api.php :

Route::get('/test','Controller@function');

邮递员

GET: localhost:8000/api/test

0
投票
add link file name  middleware crftokefile like this protected $except = [
       // 'http://127.0.0.1:8000/*'
    ];
}
after check post curl in postman
© www.soinside.com 2019 - 2024. All rights reserved.