-2

the GET requests work fine, but if i tried to POST sent... nginx vhost conf

    location /api/b1/ {
        auth_request /_validate_apikey;
        try_files $uri $uri/ /api.php?$args;
        rewrite ^/api/b1/([^/]+)/([^/]+)/?$ /api.php?data1=$1&data2=$2? last;

}

nginx log

185.111.90.223 - - [28/Oct/2022:13:16:59 +0200] "GET /api/b1/1951/ugyfel_modul HTTP/1.1" 200 16039 "-" "-"
185.111.90.223 - - [28/Oct/2022:13:16:59 +0200] "POST /api/b1/1951/adatkuldes HTTP/1.1" 200 19 "-" "-"

The request is ok apparently, no token error, but the api.php you don't get the POST array.

php curl call

$array=json_encode($array,JSON_FORCE_OBJECT);

$ch = curl_init($server.'/'.$data1.'/'.$data2);                                                               
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $array);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($array),
    'X-API-KEY:  '.$token)                                                                       
);                                                                                                                   
       
                                                                                                                     

$result = curl_exec($ch);   curl_close($ch);
$api = json_decode($result, true);  

Remote code:

if(!empty($_POST)) { 
  $json = array("ok"=>1); 
}
else {
  $json = array("ok"=>0);
}

and the answer is: ok=>0

What could be the reason why the POST data is not received? What am I not noticing? I mean an nginx configuration error....

ADyson
  • 57,178
  • 14
  • 51
  • 63
WOLFNeT
  • 23
  • 7
  • Do some more investigation. start with `var_dump($result);` ...what exactly do you see? – ADyson Oct 28 '22 at 11:39
  • P.S. Why exactly do you suspect nginx here, specifically? Maybe you did something wrong in the request, or maybe the remote server's code is faulty and sends the wrong response? We really have no idea about the scenario, so we can only speculate, without more info. Did you also attempt any debugging in `api.php` to see if it receives the request, and what it does subsequently? See also [ask] and how to create a [mre] of the issue. – ADyson Oct 28 '22 at 11:40
  • Hi, thanks... but dont have to react like that, not everyone lives their life here. :D – WOLFNeT Oct 28 '22 at 11:52
  • Remote code: if(!empty($_POST)) { $json = array("ok"=>1); }else{$json = array("ok"=>0);} and the answere is: ok=>0 – WOLFNeT Oct 28 '22 at 11:52
  • React like what exactly? I just asked you some simple questions, it's not always clear where the problem is, so you have to investigate. Once you provided more info, people were able to fix it. I'm not sure what the problem was with what I said, but please inform me if you can be more specific? – ADyson Oct 28 '22 at 12:36
  • P.S. It would now appear that this is a duplicate of [Receive JSON POST with PHP](https://stackoverflow.com/questions/18866571/receive-json-post-with-php) – ADyson Oct 28 '22 at 12:37

1 Answers1

1

POST variable is populated only when you send data using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type (check out the manual https://www.php.net/manual/en/reserved.variables.post.php). Try:

$data = json_decode(file_get_contents('php://input'), true);