java bee
java bee

Reputation: 125

Laravel Apis not working in postman in localhost

I have developed apis into php laravel, and developing on the local server and running them using

php artisan serve

these apis are working in the website but whenever i try to access them in postman it returns 419 response code with a message Page Expired.

After googling the debugging the requests from websites i found out headers to pass with the request. So i passed

_token 
X-CSRF-Token 

and also selected Bearer Token in Authorization tab. Even after adding these parameters in headers it did not work out. I tried adding them with body but it did not work there as well.

The code snippets are

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

MyUserController

 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);
            
 }

So my question is, how to make it work in local server using postman.

Upvotes: 0

Views: 10463

Answers (2)

Hemil
Hemil

Reputation: 29

Add the file name middleware/crftokenfile like this:

protected $except = [
    // 'http://127.0.0.1:8000/*'
];

Afterwards, check the POST request in Postman.

Upvotes: 0

Ahmed Amir
Ahmed Amir

Reputation: 124

use api.php file and call the route with api prefix like this :

in api.php :

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

in postman :

GET: localhost:8000/api/test

Upvotes: 3

Related Questions