Alex Ali
Alex Ali

Reputation: 1369

Cannot Receive JSON Response from Postman using Laravel API

I receive this error when I try to login with wrong credentials :

<!doctype html>
<html class="theme-light">
<!--
TypeError: Argument 2 passed to Symfony\Component\HttpFoundation\JsonResponse::__construct() must be of the type integer, array given, called in /home/stylmyvz/cars.styleshopeg.com/vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php on line 31 in file /home/stylmyvz/cars.styleshopeg.com/vendor/symfony/http-foundation/JsonResponse.php on line 42

#0 /home/stylmyvz/cars.styleshopeg.com/vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php(31): Symfony\Component\HttpFoundation\JsonResponse->__construct(Array, Array, Array)
#1 /home/stylmyvz/cars.styleshopeg.com/vendor/laravel/framework/src/Illuminate/Routing/ResponseFactory.php(99): Illuminate\Http\JsonResponse->__construct(Array, Array, Array, 0)
#2 /home/stylmyvz/cars.styleshopeg.com/app/Http/Controllers/APILoginController.php(31): Illuminate\Routing\ResponseFactory->json(Array, Array)
#3 [internal function]: App\Http\Controllers\APILoginController->login(Object(Illuminate\Http\Request)
I want to receive JSON response like this in Postman : 
{
  "error":"invalid username or password"
}

Here is Login Controller and I instruct it to give me "invalid username or password"

Can anyone tell me what is wrong with it

class APILoginController extends Controller
{
    //
    public function login(Request $request){
        $validator = Validator::make($request -> all(),[
            'email' => 'required|string|email|max:255',
            'password' =>'required'
        ]);

        if($validator->fails()){
            return response()->json($validator->errors());
        }

        $credentials = $request->only('email', 'password');
        try {
            if(! $token = JWTAuth::attempt($credentials))
            {
                return response()->json(['error'=>'invalid username or password'], [401]);
            }
        } catch (JWTException $e) {
            //throw $th;
            return response()->json(['error'=>'could not create token'], [500]);
        }

        return response()->json(compact('token'));

    }
}

Contents of api.php file:

Route::post('user/register','APIRegisterController@register');

Route::post('user/login','APILoginController@login');

Route::middleware('jwt.auth')->get('/users', function (Request $request) {
    return auth()->user();
});

Route::middleware('jwt.auth')->group( function(){
   Route::resource('/cars', 'API\CarController');
} );

Upvotes: 1

Views: 3399

Answers (2)

albus_severus
albus_severus

Reputation: 3712

Please Change this try & catch both..

try {
            if(! $token = JWTAuth::attempt($credentials))
            {
                return response()->json(['error'=>'invalid username or password'], 401);
            }
        } catch (JWTException $e) {
            //throw $th;
            return response()->json(['error'=>'could not create token'], 500);
        }

This is how I do it in Laravel....

return Response::json(['message' => $value,'other_info'=>$vlue2],201);

Or using a helper function:

return response()->json(['message' => $value,'other_info'=>$vlue2], 201); 

Upvotes: 1

Alex Ali
Alex Ali

Reputation: 1369

I found the problem it was in the error message number I passed it as an array

return response()->json(['error'=>'invalid username or password'], [401]);

and I should remove the brackets and pass it like int

return response()->json(['error'=>'invalid username or password'], 401);

Upvotes: 1

Related Questions