Php 无法使用Laravel API从邮递员处接收JSON响应

Php 无法使用Laravel API从邮递员处接收JSON响应,php,laravel,api,postman,Php,Laravel,Api,Postman,当我尝试使用错误的凭据登录时,我收到此错误: <!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

当我尝试使用错误的凭据登录时,我收到此错误:

<!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"
}
api.php文件的内容:

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

我发现问题出在我作为数组传递的错误消息编号中

return response()->json(['error'=>'invalid username or password'], [401]);
我应该去掉括号,像int一样传递它

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

请更改此选项,并尝试同时捕获两个

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(['message' => $value,'other_info'=>$vlue2],201);
或使用辅助函数:

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

您需要从PostMani发送一个POST请求。如果您要发布route.php或api.php的内容,那么它将非常有用file@SalimDjerbouh我确实发送了一个POST请求,这是我发送时收到的it@jobvink我在上面添加了api.php文件,但我不知道在哪里可以找到route.phpfile@SalimDjerbouh我用POST替换了我得到的错误要求
return response()->json(['message' => $value,'other_info'=>$vlue2], 201);