Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

HttpService:PostAsync server cross over help?

php

Data from Roblox

Data from Roblox

if($name!=NULL && $age != NULL){ $con->query("INSERT INTO Test (name,age) VALUES ('$name','$age')"); } $result = $con->query("SELECT * FROM Test"); //echo $result->num_rows." rows <br/>"; if($result->num_rows > 0){ echo("Rows: <br>"); while($row = $result->fetch_assoc()){ echo($row["name"] . ": " . $row["age"] . "<br>"); } } else{ echo("Database is empty"); } ?>

rbx

game.Players.PlayerAdded:connect(function(player)

		local s = pcall(function()
			game.HttpService:PostAsync("site",'name='..player.Name.."&age="..player.AccountAge,0,true)
		end)
	if not s then
		print("Its Broke")
	else
		print(player.Name.."'s data sent to server")
		
				
	end

end)

i have some things commented out so now one will take my server acc

please help. data sends but the server does not take it

like the stuff i have in my table shows but the stuff i sent from roblox will not i need some help

1 answer

There is a very big with SQL injections you should be using a prepared statement to avoid this problem, it is a vey easy fix.

By default the http service sends the request as Enum.HttpContentType.ApplicationJson this would mean that we need to read the data on the PHP side as a JSON which is possible.

If you want to send the data as a POST request then we need to set the content type to Enum.HttpContentType.ApplicationUrlEncoded and follow the format of the POST request like you have done above while accounting for URLEncoding.

A quick example (Lua):-

-- services
local httpServ = game:GetService('HttpService')

-- var list
local dataList = {
	['pass'] = 'test',
	['usr'] = 'aa'
}

-- return a post format string while also using url encodeing
local function createPost(tmp) -- tmp is an key value table
	local tmpStr = ''
	for key, val in pairs(tmp) do
		tmpStr = tmpStr .. httpServ:UrlEncode(key) .. '=' .. httpServ:UrlEncode(val) .. '&'
	end
	return (#tmpStr == 0) and '' or tmpStr:sub(1,#tmpStr-1) 
end

local res, data = nil, nil

local sendData = createPost(dataList)

local url = 'http://127.0.0.1'
while wait(3) do
	res, data = pcall(function() return httpServ:PostAsync(url, sendData, Enum.HttpContentType.ApplicationUrlEncoded) end)
	print('Result3', res, 'data', data)
end

PHP:-

<?php
	if (isset($_POST['usr']) && isset($_POST['pass']))
	{
		echo 'got data' . $_POST['usr'] . $_POST['pass'];
	}
	else
	{
		echo 'failed';
	}
?>

I hope this helps please comment if you do not understand how / why this code works.

This discussion is preserved as it was. New answers, comments, and votes are closed.