How to handle post requests on php requested by Roblox HttpService

Alright so i am trying to use PostAsync from HttpService to send a post request to my php file on my website but the data i put into the second parameter of PostAsync doesn’t seem to be transfered to the $_POST var.

Here’s my code:
Roblox script preforming the Post Request:

function script.Parent.OnServerInvoke(plr, Text)
	local http = game:GetService("HttpService")
	local data = {
		["text"] = Text
	}
	local response = nil
	local ok, status = pcall(function()
		response = http:PostAsync("http://mysite.com/requests/beta.php", http:JSONEncode(data), Enum.HttpContentType.ApplicationJson)
	end)
	if ok and response ~= nil then
		return response
	else
		return "error: "..status
	end
end
-- The RemoteFunction is invoked in a localscript

My php code:


<?php
if(isset($_POST["text"])){
    $newf=fopen("/stores/savedgames","w");
    fwrite($newf, $_POST["text"]);
    fclose($newf);
    echo("success");
}else{
    if(isset($_POST["text"])){
        echo("false err");
    }else{
        echo("true err");
    }
}
?>

The result will always be “true err” which means text doesn’t exist inside $_POST.

Any help would be amazing

1 Like

Please notate your code with three “grave” (`) characters before and after it. This is so code can be read with its proper indentation and character spacing, like so:

if 2 + 2 == 4 then
	for i = 1, 10 do
		print(i)
	end
end

How it looks in the text editor:
image
This will allow people to have an easier time trying to help you solve your problem.

2 Likes

I didn’t even notice that, Thanks!

(sorry for bad reply I’m rushing to school )

Hello instead of doing $_POST use $_GET :slight_smile: ( not 100% sure this would work but worth the try also you should check what method Roblox uses in HttpPost $_SERVER[“REQUEST_METHOD”] )

    $DataStoreName = $_GET['DataName'];
    $DataStoreKey = $_GET['DataStoreKey'];
    $DataValue = $_GET['Data'];
    $DataType = $_GET['Type'];

something like that :smiley:

And if it didn’t work then try using $_SERVER, it’s an array that has all the headers to check it out just do

print_r($_SERVER);

https://www.php.net/manual/en/reserved.variables.server.php

oh also I wouldn’t recommend letting the client post as many times as it wants ( since you’re putting it in a ServerInvoke it means the client can just do Rem:InvokeServer(“hi”) and it’ll do an http request )
Exploiters could use that to their advantage and just destroy your servers easily!

1 Like

Where do you decode the json string on your website?

I don’t know PHP decodes it, or my web server.

Hello! I’ve been having the same issue as well lately. However, I didn’t manage
to fix it. Just switched to get instead of post. Can you post the result if you use print_r($_POST)?

Thank you, this worked perfectly!

Weird, it should just print what gets sent but not fix your problem, lol. Anyways, glad to have been of assistance.