如何在postmen中显示jwt laravel 5.4 api中的异常



在我的laravel应用程序中,我安装了jwt包并设置了中间件,但令牌无效、过期或未发送,它显示了一个非JSON格式的错误。以下是我的问题详细信息。所有事情都正常工作,但异常不会以json格式抛出。异常作为类似这样的错误抛出(Could not decode token: Error while decoding to JSON: Malformed UTF-8 characters, possibly incorrectly encoded(,这就是问题所在。

这是我的jwtMiddleware

public function handle($request, Closure $next)
{
try 
{
$user = JWTAuth::parseToken()->authenticate();
} 
catch (JWTException $e) 
{
if ($e instanceof TymonJWTAuthExceptionsTokenInvalidException)
{
return response()->json(['status' => 'Token is Invalid']);
}
else if ($e instanceof TymonJWTAuthExceptionsTokenExpiredException)
{
return response()->json(['status' => 'Token is Expired']);
}
else
{
return response()->json(['status' => 'Authorization Token not found']);
}
return response()->json(['status' => $e]);
}
return $next($request);
} 

这是如图所示的错误显示异常为错误

和这样的预期错误要求的异常

发送邮递员请求发送给邮递员

检查您发送的请求是否具有此标头

{ Authorization: 'Bearer TOKEN' }

如何在Axios:中添加此标头

axios.interceptors.request.use(request => {
if (store.getters.authToken) {
request.headers.common['Authorization'] = `Bearer ${store.getters.authToken}`
}
return request
})

检查你的.htaccess是否有规则

RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

I show my exception in json formate below way, you can try with this. to show exception in jwt you give this in catch JWTException

$credentials = $request->only('user_name', 'password');
try {
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json([
'code'      => 404,
'response'  => 'error',
'errors'    => ['massage' => 'invalid email/ or password.']
]);
}
} catch (JWTException $e) {
return response()->json([
'code'      => 500,
'response'  => 'error',
'errors'    => ['massage' => 'failed to create token.']
]);
}

相关内容

  • 没有找到相关文章

最新更新