【问题标题】:Cannot Receive JSON Response from Postman using Laravel API无法使用 Laravel API 接收来自 Postman 的 JSON 响应
【发布时间】:2020-09-16 07:34:15
【问题描述】:

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

<!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"
}

这是登录控制器,我指示它给我“无效的用户名或密码”

谁能告诉我这是怎么回事

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

    }
}

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

【问题讨论】:

  • 你需要从 Postman 发送一个 POST 请求
  • 如果您发布您的 route.php 或 api.php 文件的内容会很有帮助
  • @SalimDjerbouh 我确实发送了一个 POST 请求,这就是我发送它时收到的内容
  • @jobvink 我在上面添加了 api.php 文件但我不知道在哪里可以找到 route.php 文件
  • @SalimDjerbouh 我用 POST 请求替换了我得到的错误

标签: php laravel api postman


【解决方案1】:

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

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

我应该删除括号并像 int 一样传递它

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

【讨论】:

    【解决方案2】:

    请更改此尝试并同时捕获两者..

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

    这就是我在 Laravel 中的做法......

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

    或者使用辅助函数:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-21
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多