How to debug API 405 calls in Laravel/Plesk

Dr. Adam Nielsen
3 min readOct 5, 2023

--

I recently wanted to set up a webhook and run into many problems. I expected a 200 response code but received a 405.

Here is what you should watch out for.

Check method exists in the routes file

A 405 response code means the method does not exist. So check if you really have the expected post or getmethod set up in your routes or API file.

Check server logs

So you made sure the method is correct in the routes file. Next, check your server access logs. In my case, I found this:

As you can see, a POST request was made to an endpoint but a 301 response was returned. This is a permanent redirect! The request was converted from POST to GET, as 301 always results in a new GET request, and the GET method is not defined in my application.

Awesome! If you find this logs, it’s only remaining to find out what is causing the redirect. (If you don’t find those logs, then check again your routes file, I have no other idea).

Check if logs end up in your Laravel app

As a first step, copy paste this code in your relevant routes file, in my case api.php :

<?php
\Log::info('API route file hit', [
'url' => request()->fullUrl(),
'method' => request()->method(),
'headers' => request()->headers->all(),
'body' => request()->all(),
]);

If you see the POST request being logged, then the redirect happens somewhere in your application. Perhaps in a Middleware? In this case, you can stop reading this article and debug your application.

If you don’t see the POST request being logged, then the redirect happens before it reaches your application. Let’s check your server settings.

Check the .htaccess file

Now it’s time to check the .htaccess file. If you are on a staging instance, I would recommend comment out everything except the line

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>

RewriteRule ^ index.php [L]
</IfModule>

The line with redirecting to index.php is important, otherwise, your Laravel app doesn’t work at all. If you still receive a 301, check your Plesk settings:

Check the Plesk Settings:

YOu may have an automatic redirect from HTTP to HTTPS, so check if you really have that https://… in front of your URL

Another thing to check, is do you have your hosting setting set up to redirect to www.* oor non-.www? This is recommended to avoid duplicate content. But it could be a reason for your 301 issues:

Did that solve your problem? If not, please leave a comment what you facing or how you solved it.

--

--

Dr. Adam Nielsen
Dr. Adam Nielsen

Written by Dr. Adam Nielsen

PHD in math. and Laravel / Vue Full-Stack-Developer

No responses yet