I have a very simple REST API script in PHP that is supposed to receive a JSON file sent via POST Rest API request from a Low-Code platform called Retool(not important to the question, but may be it will help). My goal is to process that JSON data sent and show it on the frontend on my website every time the page is opened.
<?php// /api/v1/index.phpheader("Access-Control-Allow-Origin: *");header("Content-Type: application/json; charset=UTF-8");header("Access-Control-Allow-Methods: POST, GET");header("Access-Control-Max-Age: 3600");header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");$request_method = $_SERVER["REQUEST_METHOD"];$response = "";if ($request_method == 'POST') { $data = json_decode(file_get_contents("php://input"), true); if ($data) { $response = processData($data); } else { $response = json_encode(array("message" => "Invalid input. No data received in POST request.")); }} elseif ($request_method == 'GET') { $response = json_encode(array("message" => "GET request received. This endpoint is meant for POST requests."));} else { $response = json_encode(array("message" => "Request method not supported.", "method" => $request_method));}echo $response;function processData($data) { return json_encode(array("message" => "Data received successfully.","received_data" => $data ));}?>
The issue is that when I run the post request on the Retool or test the cURL from Postman, the feedback is fine and it says
"Data received successfully."
However upon opening the actual page the message is
"Invalid input. No data received in POST request."
I found that every time you open the page directly in browser it is a GET request, hence the message, however I am confused as to how can I actually process the POST request to be able to show the data on the frontend of my page. I will appreciate any solution, but if it is compatible with my code (for example JS code or another PHP script) that would be much appreciated.
P.S. I know this is likely a Rookie question/issue however I spent several days digging and there just doesn't seem to be a solution.