php


How to catch 401 response in Laravel/Lumen?


I am writing an api for authorize users .
With my current code I can catch if response code is 200 however
I cannot catch if the response is 401 instead of 200. Instead of an error message with my 401 response, I recieve error page.
this is my swagger server in Lumen
if($input['phone']==$this->phone && $input['password'] == $this->password){
return response()->json([
'redirect_uri' => $redirect_uri,
'token' => $this->token
]);
}
return response()->json([
'redirect_uri' => $redirect_uri,
'errorMsg' => 'User Not found or id psw wrong'
],401);
and this is my model in Laravel
if($response->getStatusCode() == 401){
dd('you are not authorized');
}
if($response->getStatusCode() == 200){
dd('you are authorized');
//Store user credentials on cache
CacheStore::storeUserCredentials(json_decode((string) $response->getBody(), true));
}
basically if statuscode is 200 I recieve this message
But if status code is 401 it gives error.
Apply try catch block to your code
try{
// your api call
}
catch(Exception $e)
{
if ($e instanceof HttpException && $e->getStatusCode()== 401)
{
dd('you are not authorized');
}
}
I recommend handling universal exceptions in app\Exceptions\Handler.php.
In this file, there exists a render function.
To deal with different types of Exception, you can try:
public function render($request, Exception $e)
{
if ($e instanceof SomeTypeException1)
{
#handle it
}
else if($e instanceof SomeTypeException2)
{
#handle it
}
return parent::render($request, $e);
}

Related Links

Finding DIV's in a php | Wordpress
Cannot require parent directory in phpUnit
How to upload two files at two different paths in Yii2?
How to add “you save” option in the shopping cart of opencart?
Error 403 refreshing GMAIL token with PHP
Set Table Cell Alignment - PHPPowerPoint
doctrine:generate:crud for Entites outside a bundle
Symfony 2 lags on ubnutu and making it too slow
Strange PHP date comparison error
PHP - Escape dollar sign
sql update statement where 2 condition apllies
How to create recursive equations
How to Change the www root directory on WampServer 2.5
Insert data in clob column and varchar column in a single insert statement using PHP Oci8
Results and queries are not displaying in HTML from a MYSQL database
How to use PHP variable inside jQuery for displaying data fetched from MySQL

Categories

HOME
opencv-python
softlayer
enums
message-queue
openlayers-3
iverilog
greasemonkey
swift2
turbojpeg
video-streaming
specifications
lda
locationmanager
angular4
stimulsoft
mahout-recommender
heap-dump
gitkraken
ini
web-push
tf-idf
sendmessage
fatfs
basic-authentication
unmarshalling
xenapp
reporting
shapeless
rails-postgresql
stackpanel
cordys-opentext
llvm-ir
swfupload
data-uri
dagger
haar-classifier
nuclio
qvtkwidget
beeline
md5-file
maven-jaxb2-plugin
subforms
ptvs
haraka
ternary-operator
s-function
jsonix
dcount
decoder
traversable
fragmentstatepageradapter
deferred-rendering
wicked-pdf
node-glob
fdt
wif4.5
m4
android-mapview
nette
netdatacontractserializer
knife
ngcordova
alwayson
tree-structure
android-audiomanager
loginview
network-printers
code-complexity
miglayout
boolean-algebra
nosql-aggregation
facebook-game-groups
sql-view
quickgraph
eager-loading
delphi-xe4
system-requirements
xalan
multiscaleimage
towers-of-hanoi
nsmutablestring
android-dialog
arrow
fitch-proofs
acpi
type-safety
fork-join
grails-validation
nsconnection
3des
activestate
reliability
cleartype
mysqli-multi-query
source-code-protection
subtext
rakudo
stretchblt
chronic

Resources

Encrypt Message