简体   繁体   中英

Laravel - POSTMAN is returning 500 Internal Server Error on POST Request

I have written POST Request endpoint in Laravel-5.8 for user login. This is the location of the file:

localhost:8888/tsl-clientportal-app/clientportal-backend

AuthCoontroller.php

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use App\User;
use App\Notifications\SignupActivate;
use Avatar;
use Storage;

class AuthController extends Controller
{

    public function login(Request $request)
    {
        $request->validate([
            'email' => 'required|string|email',
            'password' => 'required|string',
            'remember_me' => 'boolean'
        ]);
        $credentials = request(['email', 'password']);
        $credentials['active'] = 1;
        $credentials['deleted_at'] = null;
        if(!Auth::attempt($credentials))
            return response()->json([
                'message' => 'Unauthorized'
            ], 401);
        $user = $request->user();

        $tokenResult = $user->createToken('Personal Access Token');
        $token = $tokenResult->token;
        if ($request->remember_me)
            $token->expires_at = Carbon::now()->addWeeks(13);
        $token->save();
        return response()->json([
            'access_token' => $tokenResult->accessToken,
            'token_type' => 'Bearer',
            'expires_at' => Carbon::parse($tokenResult->token->expires_at)->toDateTimeString(),
            'data'          => $user
        ], 200);
    }
}

api.php

Route::group([
    'prefix' => 'auth'
], function () {
    Route::post('login', 'AuthController@login');
    Route::post('signup', 'AuthController@signup');
    Route::get('signup/activate/{token}', 'AuthController@signupActivate');

    Route::group([
      'middleware' => 'auth:api'
    ], function() {
        Route::get('logout', 'AuthController@logout');
        Route::get('user', 'AuthController@user');
    });
});

When I tested the Request on POSTMAN, I expected to see the success result. But I got the error shown in the diagram below:

error

Laravel web routes requires "XSRF-TOKEN" in request header, so for testing in POSTMAN you should send "XSRF-TOKEN=value" in Cookie key in header of request.

Like: If you store your sessions in Cookies, you can grab the Cookie from an auth request in Developer Tools. 在此处输入图片说明

and then in postman在此处输入图片说明

OR you use csrf_token() value in "_token" key in header在此处输入图片说明

In general(not scoped just to this question) it can have many reason and the most common thing is that your connection to your db is disable .

For example in xampp you should start Mysql or other dbs...

Yes.its too simple but very most common mistake:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM