Laravel sanctum $request->validate not return json when validation fails

This is the first time i use Sanctum/Jetstream it and i can't understand why, during an incorrect validation $request->validate the message about validation errors in the JSON format does not come to response, but simply shows the main page. Debug mode is enabled, there are no errors in the laravel logs too. I use this example code from the official documentation, but the validation does not response JSON errors, but if the credentials I am sending are correct, i get the correct token.

Route::post('/sanctum/token', function (Request $request) {

    $request->validate([
        'email' => 'required|email',
        'password' => 'required',
        'device_name' => 'required',
    ]);


    $user = User::where('email', $request->email)->first();



    if (! $user || ! Hash::check($request->password, $user->password)) {
        throw ValidationException::withMessages([
            'email' => ['The provided credentials are incorrect.'],
        ]);
    }

    return $user->createToken($request->device_name)->plainTextToken;
});

Upvotes: 0

Views: 1111

Answers (2)

Atanu Biswas
Atanu Biswas

Reputation: 1

You just need to specify "Accept":"application/json" in Postman Headers. Just open Headers in Postmane, Add 'Accept' as a KEY and 'application/json' as Value.

Upvotes: 0

Vivek Pawar
Vivek Pawar

Reputation: 704

Did you add Accept: application/json in the header?

Upvotes: 7

Related Questions