Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What the difference between GetAsync and PostAsync?

Asked by
Kurieita 125
8 years ago

I always used GetAsync, because I thought at first 'get' oh so in php you do $_GET['Data'], but then I learned that it doesn't, it actually 'get' the data, and PostAsync, is suppose to post data.

But I did the same exactly thing with both...

local Data = HttpService:GetAsync('http://example.com') print(Data)
local Data = HttpService:PostAsync('http://example.com', "AJSONSTRING") print(Data)

On my server, I had

<?php echo 'a';

And both of them return 'a';

I also tried

local Data = HttpService:GetAsync('http://example.com?data=test') print(Data)
local Data = HttpService:PostAsync('http://example.com?data=test', "JSON") print(Data)

I could easy access 'data' on both of them by doing

$_GET['data']

but for some reason I can't access 'JSON' in the second argument of PostAsync

If someone could explain GetAsync and PostAsync and how to get data by doing $_POST['data'], that would help alot. Thanks in advance.

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

In GetAsync and PostAsync, GET and POST refer to http methods.

Confusingly, this is not what $_GET and $_POST refer to in PHP.

From the PHP documentation (emphasis mine):

$_GET: An associative array of variables passed to the current script via the URL parameters.

The "URL parameters" are the parameters following the ? in the URL. Both a POST and a GET to a URL with a query will fill in $_GET with those parameters.


$_POST gets posted data, which is sent separately, in the body.

According to this answer on Stack Overflow there are different formats that it is sent.


If you're using the default, which is Content-Type: ApplicationJSON, according to this StackOverflow answer you should use

-- PHP
$data = json_decode(file_get_contents('php://input'), true);

to get the JSON data posted.


If you want to be able to use $_POST, I believe you can set the content-type to "application/x-www-form-urlencoded" and send the data like a query string:

-- Lua:
local Data = HttpService:PostAsync('http://example.com', "first=1&second=two", "application/x-www-form-urlencoded")

-- PHP:
$myFirst = $_POST["first"] // 1
0
Wow, thanks. That really help alot :) Kurieita 125 — 8y
0
19:55:30.004 - Unable to cast string to token I am getting this error, and can't seem to figure out why. Any suggestions? Kurieita 125 — 8y
0
I don't know the line erroring, and I don't know really know HttpService well so it's possible it's a subtlety I won't recognize BlueTaslem 18071 — 8y
Ad

Answer this question