Working with REST APIs

Hi fuse,
I tried to pass the obj value to server side. But problem is API not receiving the request body. But when I use the postman app for send request it’s work properly. But here It is giving empty array. Can anyone please support me…

            function request(){

                var status = 0;
                var response_ok = false;
                var obj = {"imei":"132323"};

                fetch('http://localhost/api.php', {  
                    method: 'POST',
                    headers: {'Content-Type': 'application/json'},
                    body: JSON.stringify(obj),
                }).then(function(response) {
                    status = response.status;  
                    response_ok = response.ok; 
                    return response.json();    
                }).then(function(responseObject) {
                    // Do something with the result
                }).catch(function(err) {
                    // An error occurred somewhere in the Promise chain
                });

                }
            module.exports = {
                check
            };

        </JavaScript>
<?php
$imei = $_REQUEST['imei'];
error_log("checking imei number =>".JSON_ENCODE($_REQUEST));
if($imei=="354891062577725"){
        error_log("true");
        $data_array['status'] = true;
      echo json_encode($data_array);
}else{
        error_log("false");
        $data_array['status'] = false;
      echo json_encode($data_array);
}
?>

First, the data you’re sending is stringified. I would expect you to need to use json_decode() in PHP to get access to the body.

Next, try logging a print_r() of both $_REQUEST and $_POST, might as well give a clue on what’s incoming.

Problem is in php code. This code will fix it.
this will tell you the story.

<?php
$input = file_get_contents('php://input');
$object = json_decode($input,true);

error_log("checking imei number =>".implode(" ",$object));
$imei = implode(" ",$object);
if($imei=="3445270545445463"){
        error_log("true");
        $data_array['imeistatus'] = true;
      echo json_encode($data_array);
}else{
        error_log("false");
        $data_array['imeistatus'] = false;
      echo json_encode($data_array);
}
?>