I'm working on a REST API project with Laravel 10. Despite having properly configured the Laravel API routes for endpoints (/dp and /dj), POST requests to these two endpoints unexpectedly execute web routes rather than the expected api.php routes. Interestingly, all methods except POST for these two endpoints (/dp and /dj) function correctly, as well as POST methods for other endpoints. For example, if I hit the PUT, GET, or DELETE method for these two endpoints (/dp and /dj), they work as expected, but not the POST method. If I delete that route in the web.php, it returns a "404 not found".
Here are my routes in api.php:
Route::group(['namespace' => 'App\Http\Controllers', 'middleware' => 'auth:sanctum'],function () {
Route::apiResource('users', UserController::class)->middleware(['auth','verified','role:admin']);
Route::apiResource('mous', MouController::class)->middleware(['auth','verified','role:admin|nasabah|banker']);
Route::apiResource('office', OfficeController::class)->middleware(['auth','verified','role:admin']);
Route::apiResource('jdp', JDPController::class)->middleware(['auth','verified','role:admin|nasabah|banker']);
Route::apiResource('dp', DPController::class)->middleware(['auth','verified','role:admin|nasabah']);
Route::apiResource('jdj', JDJController::class)->middleware(['auth','verified','role:admin|nasabah']);
Route::apiResource('dj', DJController::class)->middleware(['auth','verified','role:admin|nasabah']);
});
And my web route:
Route::get('/', function () {
return view('welcome');
});
Here is my DP Controller (both /dp and /dj logic are similar so I will only show one of them):
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\DokumenPribadi;
use App\Http\Resources\DPResource;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests\StoreDPRequest;
use App\Http\Resources\DPCollection;
use App\Models\JenisDokumenPribadi;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\Response;
class DPController extends Controller
{
// Controller methods...
}
When I attempt to hit the (/dj or /dp) endpoints, it returns the view of welcome.blade.php. If I delete that route in web.php, it returns a 404 error. The routes do exist in the route list.
Here are the screenshots:
In your postman, add the following in the Headers:
{
"accept": "application/json"
}
As shown below:
You can also check out Laravel Daily's explanation on this.