I'm sending values user and id as JSON values to server using POST request.
On server I'm getting values using:
$data = file_get_contents('php://input');
$json = json_decode($data, true);
if (array_key_exists('user', $json) && array_key_exists('id', $json)) {
...
echo "received";
}
else {
...
echo "not received";
}
But it always ends in "not received".
I tried to throw new Exception to see values for $data and for $json, and here is what I got:
Uncaught Exception: $data:"{\"user\":\"usr\",\"id\":\"0\"}"
Uncaught Exception: $json:{"user":"usr","id":"0"}
It should work, but it's not working for some strange reason.
If I replace $data = file_get_contents('php://input'); with $data = "{\"user\":\"usr\",\"id\":\"0\"}";, then it works.
But if I replace $json = json_decode($data, true); with $json = "{'user':'usr','id':'0'}";, then it's not working.
I also tried to run this code in PhpStorm and it works normally. I tried replacing both $data and $json with their actual values and it works all the time, like it should work.
Does anyone have any idea why it doesn't work on the server?
EDIT:
Kotlin part:
@Headers("Content-type:application/json")
@POST("user.php")
suspend fun getUsers(@Body body: String): List<User>
val jsonObject = JSONObject()
jsonObject.put("user", user)
jsonObject.put("id", "" + id)
val users = network.getUsers(jsonObject.toString())