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

LucasCavalheri's avatar

Why am I getting a 404 for an existing route in Laravel?

Hello everyone,

I’m having an issue where a POST request to a route that I’m sure exists keeps returning a 404 error. I’ve double-checked the route definition, and it’s listed when I run php artisan route:list. Here’s what I’ve done so far:

Here’s the route defined in routes/users.php:

<?php

use App\Http\Controllers\User\CreateUserController;
use App\Http\Controllers\User\FindAllUsersController;
use App\Http\Controllers\User\GetUserController;
use App\Http\Controllers\User\ProfileUserController;
use App\Http\Controllers\User\UpdateUserController;
use Illuminate\Support\Facades\Route;

Route::name('users.')->prefix('users')->group(function () {
    Route::post('/', CreateUserController::class)->name('create');
    Route::get('/profile', ProfileUserController::class)->name('profile')->middleware(['auth:sanctum']);
    // Route::get('/{id}', GetUserController::class)->name('get')->middleware(['auth:sanctum', 'admin']);
    Route::get('/', FindAllUsersController::class)->name('all')->middleware(['auth:sanctum', 'admin']);
    Route::patch('/', UpdateUserController::class)->name('update')->middleware(['auth:sanctum']);
});

Controller code:

#[Group('Users')]
class CreateUserController extends Controller
{
    #[Endpoint('createUser', 'This endpoint is used to create a new user')]
    #[AttributesResponse([], Response::HTTP_CREATED)]
    #[AttributesResponse([], Response::HTTP_UNPROCESSABLE_ENTITY)]
    public function __invoke(CreateUserRequest $request)
    {
        $data = $request->validated();

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

        Auth::login($user);

        return $this->success('User created successfully', Response::HTTP_CREATED, UserResource::make($user))->cookie('access_token', $user->createToken('auth_token')->plainTextToken, 30);;
    }
}

When I run php artisan route:list, I see the route listed as follows:

POST       api/users ............................................................................. users.create › User\CreateUserController
  GET|HEAD   api/users .............................................................................. users.all › User\FindAllUsersController
  PATCH      api/users ............................................................................. users.update › User\UpdateUserController
  GET|HEAD   api/users/profile ................................................................... users.profile › User\ProfileUserController

When I send a POST request (I'm using INSOMNIA) to localhost:8000/api/users, I get a 404 error, but instead of a JSON response, Laravel is returning an HTML page with a "Page Not Found" message, as if I’m accessing the application via a web browser.

1 like
3 replies
Snapey's avatar
Snapey
Best Answer
Level 122

make sure you send Accept: application/json header if you want to have a json response

It might be a validation error which is trying to return to the posted form

1 like
LucasCavalheri's avatar

@Snapey Thank you, Snapey! That was indeed the issue — adding the Accept: application/json header resolved the problem.

Interestingly, up until recently, I didn't have to manually add this header in tools like Insomnia, and Laravel automatically returned JSON responses for API routes. Do you know if there's been any change in Laravel's behavior regarding this?

Also, is there a way to make Laravel automatically respond with JSON for all API routes without explicitly setting the header? My concern is that if I enforce this globally, it might interfere with routes that rely on multipart/form-data for file uploads or similar use cases.

Any insights would be greatly appreciated!

Please or to participate in this conversation.