Search code examples
laravel-8laravel-7

405 Method Not Allowed laravel 7


I have an API Rest developed in laravel 7.2 hosted in hostinger when I make a query to a GET method everything works fine, when I use another method like POST or PUT I get this error 405 Method Not Allowed. On a local server it works fine. I am using Postman to test the API.

I attach the API code from the routes/api.php file.

<?php
use App\Http\Controllers\API\AuthController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::post('prueba', [AuthController::class,'prueba']);
Route::get('prueba2', [AuthController::class,'prueba2']);

Annex API code of the AuthController class.

<?php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Support\Facades\Validator;

class AuthController extends Controller
{

    public function prueba(Request $request)
    {
        return response()->json(['message' => 'Hello Word']);
    }
    
     public function prueba2()
     {
         return response()->json(['message' => 'Hello Word2']);
      }

}

Annex .htaccess file

RewriteEngine On 
RewriteCond %{REQUEST_URI} !^public 
RewriteRule ^(.\*)$ public/$1 \[L\]

Attached images of the tests

This error does not occur on localhost and does not occur with the GET method.


Solution

  • something similar happened to me a couple of times when I tried to create new routes but my problem was that the route cache wouldn't let me make the new routes valid. I used this command to clear the cache and it worked, maybe that's it.

    php artisan route:clear
    php artisan route:cache
    

    ` or you can also create a route to clear the route cache.

    //Clear route cache:
    Route::get('/route-cache', function() {
     Artisan::call('route:cache');
     return 'Routes cache has been cleared';
    });`
    

    Well, I was checking and testing and if the request is made via https it doesn't work but via http it does, which indicates to me that there is a problem in the https redirection of the server or something related.

    enter image description here