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
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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.
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
Please or to participate in this conversation.