Setup
Apache Virtual Host configured for: https://api.mydomain.com
SLIM Setup installed in the root of the web (DocumentRoot) as this Virtual Host is for
API endpoints only (no shared web content)
Setup taken from : Installation - Slim Framework
DocumentRoot
|
+--- composer.json
+--- composer.lock
+--- vendor
+--- public // GET Endpoint (OK)
| +--- index.php
|
+--- geotracking // POST Endpoint (Error 500)
+--- index.php
Setup commands used:
# composer require slim/slim:"4.*"
# composer require slim/psr7
# mkdir public
# mkdir geotracking
# cd public
Created an index.php with the following GET example
----- public/index.php ----------------------------------------------------------
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
$app = AppFactory::create();
$app->setBasePath('/public');
$app->get('/', function (Request $request, Response $response, $args) {
$response->getBody()->write("Hello world (PUBLIC)!");
return $response;
});
$app->run();
---------------------------------------------------------------------------------
Calling https://api.mydomain/public works flawlessly and returns âHello World (PUBLIC)!â
Using PowerShell 7 with
PS> Invoke-RestMethod -Uri "https://api.mydomain.com" -Method Get
returns âHello World (PUBLIC)!â as well.
I can do similar GET examples in the âgeotrackingâ subfolder by simply adjusting the
setBasePath(â/geotrackingâ) and calling the URI
https://api.mydomain.com/geotracking
So far so good. Now I wanted to do a POST sample which simply echoâs a JSON-Body
back to the caller.
!!! And here everything breaks with Apache returning error 500.
I have no clue why. I spent hours and I donât see the problem.
----- geotracking/index.php -----------------------------------------------------
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Slim\Exception\HttpBadRequestException;
require __DIR__ . '/../vendor/autoload.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);
$app = AppFactory::create();
$app->setBasePath('/geotracking');
$app->addBodyParsingMiddleware(); // Enable JSON parsing
// Enable error details (only for development!)
$errorMiddleware = $app->addErrorMiddleware(true, true, true);
//$app->map(['GET', 'POST']'/', function (Request $request, Response $response) {
$app->post('/', function (Request $request, Response $response) {
$data = $request->getParsedBody();
if (!is_array($data)) {
$response->getBody()->write(json_encode(['error' => 'Invalid JSON']));
return $response->withHeader('Content-Type', 'application/json; charset=utf-8')->withStatus(400);
}
$response->getBody()->write(json_encode($data));
return $response->withHeader('Content-Type', 'application/json; charset=utf-8');
});
---------------------------------------------------------------------------------
Then I call the endpoint from PowerShell 7 with the following code sequence and I get an error 500.
----- PowerShell invocation -----------------------------------------------------
$uri = 'https://api.mydomain.com/geotracking'
$headers = @{
"Content-Type" = "application/json; charset=utf-8"
}
$body = @{
"name" = "John"
"age" = 30
}
# Convert the body to JSON format
$jsonBody = $body | ConvertTo-Json
# Make the REST API call
$response = Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body $jsonBody
Invoke-RestMethod -Uri $uri -Method Post -Body $jsonBody -Verbose
Output
PS> Invoke-RestMethod -Uri $uri -Method Post -Body $jsonBody -Verbose
VERBOSE: Requested HTTP/1.1 POST with 36-byte payload
VERBOSE: Received HTTP/1.1 response of content type text/html of unknown size
Invoke-RestMethod: 405 Method Not Allowed body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana,sans-serif} h1{margin:0;font-size:48px;font-weight:normal;line-height:48px} strong{display:inline-block;width:65px} 405 Method Not Allowed The application could not run because of the following error:DetailsType: Slim\Exception\HttpMethodNotAllowedExceptionCode: 405Message: Method not allowed. Must be one of: POSTFile: /user1/WWWROOT/api.mydomain.com/html/vendor/slim/slim/Slim/Middleware/RoutingMiddleware.phpLine: 79Trace#0 /user1/WWWROOT/api.mydomain.com/html/vendor/slim/slim/Slim/Routing/RouteRunner.php(62): Slim\Middleware\RoutingMiddleware->performRouting()
#1 /user1/WWWROOT/api.mydomain.com/html/vendor/slim/slim/Slim/Middleware/BodyParsingMiddleware.php(65): Slim\Routing\RouteRunner->handle()
#2 /user1/WWWROOT/api.mydomain.com/html/vendor/slim/slim/Slim/MiddlewareDispatcher.php(129): Slim\Middleware\BodyParsingMiddleware->process()
#3 /user1/WWWROOT/api.mydomain.com/html/vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(77): Psr\Http\Server\RequestHandlerInterface@anonymous->handle()
#4 /user1/WWWROOT/api.mydomain.com/html/vendor/slim/slim/Slim/MiddlewareDispatcher.php(129): Slim\Middleware\ErrorMiddleware->process()
#5 /user1/WWWROOT/api.mydomain.com/html/vendor/slim/slim/Slim/MiddlewareDispatcher.php(73): Psr\Http\Server\RequestHandlerInterface@anonymous->handle()
#6 /user1/WWWROOT/api.mydomain.com/html/vendor/slim/slim/Slim/App.php(209): Slim\MiddlewareDispatcher->handle()
#7 /user1/WWWROOT/api.mydomain.com/html/vendor/slim/slim/Slim/App.php(193): Slim\App->handle()
#8 /user1/WWWROOT/api.mydomain.com/html/geotracking/index.php(32): Slim\App->run()
#9 {main} Go Back
As you can see I tried as well with
$app->map([âGETâ, âPOSTâ]â/â, function (Request $request, Response $response) {
but this doesnât work neither.
Help / Ideas ?
So any idea why such a simple POST example does not work ?
Why does it throw a 405 Method not Allowed???
I am out of ideas for the moment
Regards,
Oliver