Laravel Http Response Issues

Created At: 2023-02-15 19:01:34 Updated At: 2024-03-25 21:00:14

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.

Learn how to exactly catch error in Laravel

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 Status:401 Unauthorized

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 app\Http\kernel.php file.

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.

Comment

Add Reviews