POST Data from HTTP services doesn't show up in my $_POST variable in PHP

I’m trying to send data to my php server but post data doesn’t show up in the $_POST[‘user’] variable. It would always return ‘failure’.

ROBLOX SIDE:

local HttpService = game:GetService("HttpService")

local URL = "https://phpwebsite/listener.php"

data = {
	['user'] = "bob"
}

jsonData = HttpService:JSONEncode(data)
local response = HttpService:PostAsync(URL, jsonData, Enum.HttpContentType.ApplicationUrlEncoded, false)

jsonData = HttpService:JSONDecode(response)

print(jsonData.result)

PHP SIDE:

<?php
 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
   echo "This API is for Post Method.";
 }
 else{
     if(ISSET($_POST['user'])){
         $result = $_POST['user'];
         echo json_encode(['result' => $result]);
     }
     else{
         echo json_encode(['result' => 'failure']);
     }
     
 }
?>

The content-type of your request is ApplicationUrlEncoded when it should be ApplicationJson, because you send JSON to server

If I’m right it should be:

local HttpService = game:GetService("HttpService")

local URL = "https://phpwebsite/listener.php"

data = {
	['user'] = "bob"
}

jsonData = HttpService:JSONEncode(data)
local response = HttpService:PostAsync(URL, jsonData, Enum.HttpContentType.ApplicationJson, false)

jsonData = HttpService:JSONDecode(response)

print(jsonData.result)

Also this should be asked in scripting-support, not dev discussion

Sorry, I’m pretty new to the website.

I tried ApplicationJson as the content-type but it still results in ‘failure’.
I read from other posts that I should use ApplicationUrlEncoded which didn’t work so here I am stuck at this problem.

you can try debug it by using echo in php to see what server has request body

How do I do that?
because what I’m doing is I’m sending a json from roblox to my php server and then printing the ‘result’ on roblox studio?

no i mean php has function
echo var_dump($_POST);

then check result on roblox 3921390

Do you mean like this?

yes, but why there no any results, what is ur current php code now

Oops my bad, I did it wrong.

Current PHP Code:

<?php
 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
   echo "This API is for Post Method.";
 }
 else{
     $result = $_POST['user'];
     var_dump($result);
 }
?>

Current Roblox Code

local HttpService = game:GetService("HttpService")

local URL = "https://website/listening.php"

data = {

["user"] = "Zenovain"

}

jsonData = HttpService:JSONEncode(data)

local response = HttpService:PostAsync(URL, jsonData, Enum.HttpContentType.ApplicationJson, false)

print(response)

CURRENT ERROR IN STUDIO

can u try to var_dump $_POST, not $_POST[user]

image

Php Code

<?php
 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
   echo "This API is for Post Method.";
 }
 else{
     $result = $_POST;
     var_dump($result);
 }
?>