Jwt-auth: The GET method is not supported for this route. Supported methods: POST - Laravel 5.8

Created on 9 Jul 2019  路  32Comments  路  Source: tymondesigns/jwt-auth

The GET method is not supported for this route. Supported methods: POST

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.

My environment

| 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.

Most helpful comment

Just make sure headers accept value set to application/json.
content

and now everything works fine.

All 32 comments

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.

  1. app > Http > Middleware > Authenticate.php
  2. redirectTo func in route('login') to route('change route name')

  3. 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-

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Session;
use Stripe;
use DB;

class StripePaymentController extends Controller
{
/**
* success response method.
*
* @return \Illuminate\Http\Response
*/
public function stripe(Request $request)
{

$session_id=$_POST['session_id'];
$class_id=$_POST['class_id'];
$booking_id=$_POST['booking_id'];   

return view('stripe',compact('session_id','class_id','booking_id'));

}

/**
 * success response method.
 *
 * @return \Illuminate\Http\Response
 */
public function stripePost(Request $request)
{

   //print_r($_POST); die;

    Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));

    Stripe\Charge::create ([
            "amount" => 100 * 100,
            "currency" => "usd",
            "source" => $request->stripeToken,
            "description" => "Test payment from itsolutionstuff.com.",
            'shipping' => [
            'name' => 'Jenny Rosen',
            'address' => [
              'line1' => '510 Townsend St',
              'postal_code' => '98140',
              'city' => 'San Francisco',
              'state' => 'CA',
              'country' => 'US'
            ],
          ],
    ]);

    Session::flash('success', 'Payment successful!');

    return redirect()->back();
}

}

?>

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.
content

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.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.

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.

@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'
image

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']);

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shah-newaz picture shah-newaz  路  3Comments

therealmjk picture therealmjk  路  3Comments

heroghost picture heroghost  路  3Comments

kofi1995 picture kofi1995  路  3Comments

marciomansur picture marciomansur  路  3Comments