laravelroutesmethods

POST: register return The GET method is not supported for this route. Supported methods: POST in laravel-api 8


After I deployed my project to server the Route: register start return this:

"message": "The GET method is not supported for this route. Supported methods: POST.",
"exception": "Symfony\\Component\\HttpKernel\\Exception\\MethodNotAllowedHttpException",

in localhost, it worked succesful!

My route list is:

| POST| api/register | App\Http\Controllers\Auth\UserAuthController@register

My route in api.php:

Route::post('register', [UserAuthController::class, 'register']);

Controller:

class UserAuthController extends Controller
{
    public function register(Request $request)
    {
        $data = $request->validate([
            'name' => 'required|max:255',
            'email' => 'required|email|unique:users,email',
            'password' => 'required|confirmed',
            'phone_number' => 'required|numeric|unique:users,phone_number',
            'country' => 'required|string',
            'city' => 'required|string',
            'address' => 'string',
            'VIP' => 'boolean'
        ]);

        $data['password'] = bcrypt($request->password);

        $user = User::create($data);

        $token = $user->createToken('API Token')->accessToken;

        return response(['user' => $user, 'token' => $token], 201);
    }

Solution

  • The problem was from the host not the project, When you face this problem and the project worked in your local host but facing this problem in the server just check if you enable HTTPS protocol in cPanel or in .htacsses in the root path.

    After you do this your project should work successfully.