Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cola's avatar
Level 1

How can I make post api to login? "The POST method is not supported for this route. Supported methods: GET, HEAD"

Url:

http://127.0.0.1:8000/api/[email protected]&password=secret

api.php

Route::get('/login', [UserController::class, 'login']);

It works.

But making a post api, like in api.php

Route::post('/login', [UserController::class, 'login']);

It does not work. I have tested in postman adding json data like in body.

{
"email":"[email protected]",
"password":"secret"
}

I get error:

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: The POST method is not supported for this route. Supported methods: GET, HEAD. in file C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php on line 117

UserController.php

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use App\Http\Resources\UserResource;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use App\Models\User;

class UserController extends Controller
{
    public function login(Request $request) {  
        
        $email=$request->input('email');
        $password=$request->input('password');        
        $model = User::where('email', '=', $email)->first();
        //dd($email);    
        if (!empty($model)) {
            $cryptedpassword=$model->password;
            if (Hash::check($password, $cryptedpassword)) {
                $user=[
                    "name" => $model->name,
                    "email" => $model->email
                ];
                $data=[
                    "status" => "Success",
                    "api_token" => Str::random(60),
                    "user" => $user
                ];
            } else {
                $data=[
                    "status" => "Password did not match.",
                    "user" => null
                ];
            }
        } 
        if(empty($model)) {
            $data=[
                "status" => "Email did not match.",
                "user" => null
            ];
        }        
        return response()->json($data);
      }
    
}
0 likes
4 replies
cola's avatar
Level 1

Thank you. Clearing routes work.

Sinnbeck's avatar

Don't cache your routes on your dev pc. And please mark the answer by Michal as best

Please or to participate in this conversation.