I want to create an API POST handler on Fat-Free Framework.
When I send any test POST request inside the application, nothing happens, can't find the reason for the error.
Sending a request
$dataContainers = [
'type' => 1,
'units' => 1,
'container' => 1,
];
$query = http_build_query($data);
$options = ['http' => [
'method' => 'POST',
'header' => ['Content-Type: application/json; charset=utf-8'],
'content' => $query,
],
];
$context = stream_context_create($options);
$response = @file_get_contents('/api/packer/containers', false, $context);
Routing .ini
POST /api/packer/containers = App\Controller\Packer\PackerApiController->setContainers
Controller
class PackerApiController
{
private $data;
public function __construct($f3, $params)
{
// Trying to test get and return any data
$params; // empty
$post = json_decode(file_get_contents('php://input'), true); // empty
$post = json_decode($f3->get('BODY'), true); // empty
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-Type: application/json; charset=utf-8');
return json_encode($post);
}
}
You defined the route to point to a
setContainersmethod of thePackerApiController. But then you try to handle the request in the constructor of this class. Create the method and move the code to it.