I would like to add a post endpoint for my plugin (octobercms 4).
I use GET endpoints since a while and they work perfectly fine. However, for some reasons, POST is a 404 as a result.
Currently, I registered the endpoint in the routes.php of my plugin. The GET endpoints are there as well and they work just fine:
Route::post('/api/test', function () {
trace_log("POST");
return response()->json('Hello POST');
});
When I type php artisan route:list, the route is listed with POST, so the route is definitly known.
But the code is never reached.
I test the requests with Postman. What did I do wrong?
It might be getting blocked by CSRF before it even reaches the route. Try using the api middleware group.
Route::group(['middleware' => ['api']], function () {
Route::post('/api/test', function () {
trace_log("POST");
return response()->json('Hello POST');
});
});
1 Like
Route::group(['middleware' => ['api']], function () {
trace_log("check the middleware");
Route::post('/api/test', function () {
trace_log("post");
return response()->json('Hello POST');
});
});
The “check the middleware” was logged, the “post” was not logged.
does it work for you?
Change it to Route::any instead of post, if it works, then you’re not submitting a POST request.
Also trace_log(request()->method()) to see what it actually is.
1 Like
Erm… wow. My method is get, even if I use “Post” in the Dropdown within Postman o_O
So it’s definitly not an OctoberCMS problem. Still, do you have any idea why I send a get even if I selected “POST” o_O
I tested POST requests on other services and on a random html page - they arrived as they should.
Any ideas on how to find out, where the POST request was converted to GET are welcome.
(sorry for the doule post, something went wrong with an edit)
I found the solution: I was sending my local requests with http:// instead of https:// accidently
Since I force https:// with htacces, the request was redirected and therefor changed from POST to GET.
So: I switched the postman url to https:// and the request is no longer modified.
1 Like