I am trying to get a response in dev.site.com.br/api/auth/me and I have the error
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: The GET method is not supported for this route. Supported methods: POST. in file C:\xampp\htdocs\rnlinked\api\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php on line 256
I am using laravel 5.8 and JWT 1.0
The login method works fine, but the me or logout methods doesn't work.
| Bug? | yes
| Framework | Laravel
| Framework version | Laravel Framework 5.8.27
| Package version | "tymon/jwt-auth": "dev-develop"
| PHP version | 7.3.3
Below are my codes:
AuthController.php
<?php
namespace App\Http\Controllers\Api;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class AuthController extends Controller
{
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
public function login()
{
$credentials = request(['email', 'password']);
if (! $token = auth()->attempt($credentials)) {
return response()->json(['error' => 'Invalid Login'], 401);
}
return $this->respondWithToken($token);
}
public function me()
{
return response()->json(auth()->user());
}
public function logout()
{
auth()->logout();
return response()->json(['message' => 'Successfully logged out']);
}
public function refresh()
{
return $this->respondWithToken(auth()->refresh());
}
protected function respondWithToken($token)
{
return response()->json([
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => auth()->factory()->getTTL() * 60
]);
}
}
In routes/api.php
Route::group([
'middleware' => 'api',
'prefix' => 'auth'
], function ($router) {
Route::post('login', 'Api\AuthController@login')->name('login');
Route::post('logout', 'Api\AuthController@logout')->name('logout');
Route::post('refresh', 'Api\AuthController@refresh')->name('refresh');
Route::post('me', 'Api\AuthController@me')->name('me');
});
I am using postman and I do a POST request on logout or me or refresh and they get the same error. Only the login method works.
I've done a similar problem, when I created a route in GET and then I went to POST it cleans the laravel cache
Try using the following code in your project:
php artisan cache:clear
php artisan route:cache
php artisan config:cache
php artisan view:clear
I've done a similar problem, when I created a route in GET and then I went to POST it cleans the laravel cache
Try using the following code in your project:php artisan cache:clear php artisan route:cache php artisan config:cache php artisan view:clear
Thanks for the tip. But it didn't work for me =/
@pereiracinthiag in your routes/api.php you've set up all the routes as POST routes... which is why it is not allowing GET. If you want to use GET for me, you'd have to change it to this:
Route::group([
'middleware' => 'api',
'prefix' => 'auth'
], function ($router) {
Route::post('login', 'Api\AuthController@login')->name('login');
Route::post('logout', 'Api\AuthController@logout')->name('logout');
Route::post('refresh', 'Api\AuthController@refresh')->name('refresh');
Route::get('me', 'Api\AuthController@me')->name('me');
});
It's pretty standard to use POST for logout.
@pereiracinthiag Try to solve your problem like this.
redirectTo func in route('login') to route('change route name')
routes > api.php
make route
Route::get('change route name'), function (){
return response()->json(['error' => 'unauthenticated']);
});
@pereiracinthiag I came across this issue before in 5.8, consider specifying request method type in your html tag <html method="POST"> rather than using @method('POST').
Error still not fixed
It work for me. Change the 'middleware' => 'api', to 'middleware' => 'jwt.auth'
i have the same error but in my case, the error only occurs when i Clicked the URL and pressing enter. the error shows, but if hitting f5 the page refreshes normally no error at all. how to fix this error please help. im using POST method.
Go to vendor>Laravel>framework>src>illuminate>routing>Router.php
Change line 1154:
$this->post('logout', 'Auth\LoginController@logout')->name('logout');
to
$this->get('logout', 'Auth\LoginController@logout')->name('logout');
This solved the problem for me!
@konte21 Sir still doesn't work, maybe because my vendor folder greyed and when i changed the line 1154 it didn't take effect.
my scenario is like this, i have blade template and has a form and uses post method action="home/something", after clicking submit, the current page is in /home/something and displays some information but when i click the url bar and the url is all selected then pressing button enter the "The GET method is not supported for this route. Supported methods: POST." shows.
Any update to this? I am also experiencing the same issue.
Any update to this? I am also experiencing the same issue.
This happened to me today and it was a really silly mistake... I was using http instead of https on my API requests
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.
I am using the Stripe payment gateway when I click on Pay then I am getting this error also-
my controller cose here-
form route-
The problem is with authentication. The routes are fine..See the auth type you are using and provide the credentials for you to hit that Get or Post end point.
https://laracasts.com/discuss/channels/laravel/routing-in-laravel-6-methodnotallowedhttpexception
Just make sure headers accept value set to application/json.
and now everything works fine.
I think you haven't set token while sending a request so you should be sure to send with token for requests such as logout, me, refresh
@pereiracinthiag
I also face similar error
"GET method is not supported for this route. Supported methods: POST."
I create the new route for that method/function with a different name and it works.
Any update to this? I am also experiencing the same issue.
This happened to me today and it was a really silly mistake... I was using http instead of https on my API requests
This solved my problem! Thanks!
Can you explain how to change http to https on the API rest.
Thanks in advance!
Can you explain how to change http to https on the API rest.
Thanks in advance!
I just replaced http with https in URLs.
Thanks for your response. In fact,I am using https. The error appears when I do the laravel the validation of the form. So, at the end I decided do the validation using Jquery.
@pereiracinthiag in your
routes/api.phpyou've set up all the routes asPOSTroutes... which is why it is not allowingGET. If you want to useGETforme, you'd have to change it to this:Route::group([ 'middleware' => 'api', 'prefix' => 'auth' ], function ($router) { Route::post('login', 'Api\AuthController@login')->name('login'); Route::post('logout', 'Api\AuthController@logout')->name('logout'); Route::post('refresh', 'Api\AuthController@refresh')->name('refresh'); Route::get('me', 'Api\AuthController@me')->name('me'); });It's pretty standard to use
POSTfor logout.
This Solution worked for me thanks
Try this, go to file
vendor/laravel/framework/src/Illuminate/Routing/CompiledRouteCollection, line 159
Replace the code with the following:
$trimmedRequest->server->set('REQUEST_URI', $parts[0].(isset($parts[1]) ? '?'.$parts[1] : ''));
It is not a good choice to change the vendor codes, but until the correction comes out this may be an alternative.
Same issue here.
Here's my stackoverflow question: https://stackoverflow.com/questions/63389765/api-405-method-not-allowed-on-laravel-7-with-tymon-jwt-auth?noredirect=1#comment112089868_63389765
@pereiracinthiag in your routes/api.php you've set up all the routes as POST routes... which is why it is not allowing GET. If you want to use GET for me, you'd have to change it to this:
Route::group([
'middleware' => 'api',
'prefix' => 'auth'
], function ($router) {
Route::post('login', 'Api\AuthController@login')->name('login');
Route::post('logout', 'Api\AuthController@logout')->name('logout');
Route::post('refresh', 'Api\AuthController@refresh')->name('refresh');
Route::get('me', 'Api\AuthController@me')->name('me');
});
This worked for me too. Thanks
Route::get('/logout', 'Auth\LoginController@logout');
Route::get('/logout', 'Auth\LoginController@logout');
That won't work very well for Laravel 8, see Basic Routing
Route::get('/logout',[LoginController::class, 'logout'] );
just make sure that Headers accept application/json
and also make sure that the route grouping is
'middleware' => 'api',
'prefix' => 'auth'

This worked for me
class AuthController extends Controller
{
/**
* Create a new AuthController instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login','register','logout','me','refresh']]);
}
i got same error on my hosting which is on linux i change my URL from http://your-domain.com/api/post-test/ to http://your-domain.com/api/post-test i just remove / and it works like charm
Route::get('/logout', 'Auth\LoginController@logout');
work for me in Laravel 8 using :
Route::get('/logout', [LoginController::class, 'logout']);
Most helpful comment
Just make sure headers accept value set to application/json.

and now everything works fine.