Complete BLoC with Clean Architecture (group chat) Discount !! E-commerce App With Backend Source Code Video and Voice Chatting App Firebase Chatting App Source Code Complete Gym App BLoC State Management Source Code Complete Study App Buy Ticket Booking App Source Code Buy Travel App With Backend Source Code Complete Chat App Udemy Course Special Offer Discount !! Online Learning Course App (BLoC) Online Learning Course App (Riverpod) Online Learning Course App (Getx) Discount !! Shopping App (Provider) Cool Flutter Game Flutter Nodejs Chat App Flutter Nodejs Api And Firebase Chat App Riverpod Task Management App
Here we will cover different kind of api request error during api call. Even though this tutorial is about laravel but the error reporting is same for Nodejs or Python or any other framework. Here we will see error 400, 401, 429, and 500.
400 Bad Request
During app building or api building process you may encounter 400. Many 400 bad request happens due to authorization header which requires for special access to resources. In other words, your Bearer token is wrong or expired. At the same some framework may through error by saying
"error": "Route [login] not defined."
See from the above Postman request, we get 400, at the same we see Route login is not defined. It is just due to authorization header or Bearer token have expired. Update the header in the authorization section of the postman and you are good to go.
401 Http Response
It happens because your http request header is containing wrong information. Like if you are sending a
token and if the token is wrong, It will get 401. This is also known as Unauthorized.
Or if your client needs token and and you are not sending it, you will get 401.
You see Postman at the top right corner, returned
429 Http Response
Let's see what
429 issues. It means a client (our app) is making too many requests per minute to Framework. It happens, framework and server both want to protect themselves. So it tells you are doing 429.
It's a basic set up against DDoS attack. It restricts certain IP, how times they can can visit the framework.
Well, we can tell Laravel framework that we want to certain limit for http request per minute. We could do the settings in
From the above picture you may see I added a new value in the api list. Here I added throttle:180, 1
This tells, Laravel framework that certain client from a certain IP can make up to 180 requests per minutes. You may do much less if you want.
404 Error Not Found
You may often encounter if you that means your request endpoint is wrong. You may check your routes.php file to check if you have a correct endpoint or not.
In general, if you don't have defined endpoint or your endpoint's name is wrong you get this error 404.
404 Error with Ngrok
If you use Ngrok for localhost expose to outside world, you might get 404 error. That could happen due to wrong port number. In general Laravel always runs on port like 8000, 8001, 8002...
But Ngrok could be like
ngrok http 80 which is port 80. That's the default of Ngrok port. Make sure you change it to 8000 or your prefered port number.
Watch this Laravel and Ngrok video
With Ngrok you may also get 500 error as below.
Invalid URL success. URLs must begin with http or https
Once again there could be many reasons for it. But in my case my .env file was not reading set url correctly.
To solve this issue I had run the below command
php artisan config:clear
500 Internal server
You get this error for many reasons. It could be syntax error, wrong data passed, database field mismatch error, duplicate entry of email or identity or for primary key.
Here we get this error 500, because of duplicate error email account. In our database this email account already exist, so you can not re-register the account holder.
If you remove the the row related to the email in the table, you will not see this error.