Help on : How to send table of data through gmodlua: http.Post

Hi the comunity and hi glua developpers,
I am building a gmod program who’s send data through http.Post to get the current online players.

My API needs a parameters which is an array of steamIds.


api.example.com/connect?steamIds=STEAM_0:1:127192166&steamIds=STEAM_0:0:145414217

The api send back to players an array of steamIds with some other data (but this is not the problem)

In GMODLUA documentation we can see that http post is waiting a table of parameters:

http.Post( string url, table parameters, function onSuccess=nil, function onFailure=nil )

In my script we can see :


playerRequest = {}

for k, ply in pairs( player.GetAll() ) do
        local steamID = ply:SteamID()
        local text = 'steamIds ='..steamID
        table.insert(playerRequest, text);
end

http.Post("http://api.example.com/connect",playerRequest,
        function( body, len, headers, code )
        	HIDDEN Code HERE
        end,
        function( error )
                -- ply:ChatPrint("Api error")
                -- We failed. =(
        end
)


On access log i can see that API read steamIds as an undefined parameters.

Please note that others calls like the following one with post in my script works well :


http.Post("http://api.example.com/storeData",{steamId = ply:SteamID(), steamName = ply:SteamName(), userName= ply:Nick()},function()
        end
)

My problem is only about how send a parameters who’s an array of parameters through post.
There is no example in gmod docs for it and google for this problem is not my friend yet.

Can someone can help me ?

Best Regards,
Please forgive me for my bad english, i am french.

Cordially,
MadeByHost

Print your table and you’ll see why it doesn’t work.

Hint: Look at your indexes.

Forgot to mention - don’t sign your posts, we can see who posted it FYI.

In the example of your url you’re using query strings to send data, not post. That and if you’re using the post method then the way you’re indexing your table is messed up.

I already have printed the table.
Indexes are 1 , 2 with 1 => steamIds = STEAM_0:0:145414217,
I have try the fact of setting playerRequest[‘steamIds’] = STEAM_0:0:145414217 or playerRequest[#playerRequest +1] = STEAM_0:0:145414217 but that doesn’t allow to create a table like it:


{steamId = STEAM_0:0:145414217, steamId = STEAM_0:0:145414217, ....} // That's playerRequest value is attending to getting call work

Any Ideas about how to solve it ?

[editline]3rd November 2015[/editline]

Please remember that my needs is to get a post that match the following logic (don’t care about value on wich is an http GET, it’s just to show you that the api is waiting an array named steamIds containing each steamId of players on the server):
api.example.com/connect?steamIds=STEAM_0:1:127192166&steamIds=STEAM_0:0:145414217

[editline]3rd November 2015[/editline]

I’ll try that as soon as i come back to my home, i ll send you if it works



playerRequest = {}
playerRequest["steamIds"] = {}
for k, ply in pairs( player.GetAll() )
do 
   table.insert(playerRequest["steamIds"], ply:SteamID())
 
end


You dont seem to have a grasp on variables. If you name it the same it will just overwrite it. I would just look at making the table json and then sending that as a variable to your script.
IE



playerRequest = {}

for k, ply in pairs( player.GetAll() ) do
    table.insert(playerRequest, ply:SteamID())
end
playerRequest = util.TableToJSON(playerRequest)

local postVars = {players = playerRequest, someother = "Just another thing"}
http.Post("http://api.example.com/connect", postVars ,
        function( body, len, headers, code )
        	HIDDEN Code HERE
        end,
        function( error )
                -- ply:ChatPrint("Api error")
                -- We failed. =(
        end
)


After that if you are using php to treat the results you can do:



$players = json_decode($_POST['players'])


You’re sending a table with the indexes pointing to strings like: “steamId = STEAM_0:1:234567”. The http.Post call that works has a table where there is an index steamId and it points to ply:SteamID(). Your api expects a table index called ‘steamId’ which points to a string of the players steamid, but instead you are passing a table index called ‘1’ which points to the string “steamId = STEAM_0:1:234567”.

If you want a table in the form of {steamId = ply:SteamID(), steamName = ply:SteamName(), userName= ply:Nick()}, then you need to do to:


playerRequest["steamId"] = ply:SteamID()

repeat that for all your other indexes.

@andreblue This way is better.
I ll test it ass soon as i come back Home;

PS: I am using a NODEJS api, and i am a webDev atm @ work, just experiencing some miss-understanding about glua and how to pass this array of steamIds into the api from a glua script.

Now with @andreblue, i think i am on the good way.

For @roastchicken, please read my first posts please instead of post something that works weel for me atm. Look my first post , i want to know how to pass an array of steamIds with the POST Method.
The example on the first post was to show that i already know how to pass multiple determined variable through a POST. This one is about non determinate array of SteamIds.

For Both and all whos participate @ this post please consider that i say you all “Merci (Thanks)”.
I come back in few hours after testing @andreblue solution and then post the solve into that thread if it works.

Before I can give any advice I have to know if you’re get or post for getting the data, as you stated in your first post you want it to work like this with query strings…


api.example.com/connect?steamIds=STEAM_0:1:127192166&steamIds=STEAM_0:0:145414217

But in your example code you’re using post to send the data, so whatever api you’re using wouldn’t be able to read any data from it if you’re using get, also If you’re going with the above approach then you don’t need to use the http.Post function.

As for sending the data for over get, just append a string with the key and steam id and append that to your url string, as you with your current example you’re overwriting the steamIds variable.
As for sending the data over post, just just add all of the steams ids to a table and make sure all of the keys are strings.

Before I can give you examples of either above it’d be nice to know what method you’re using.

@bigdogmat
As i was explain in my second post : http://forum.facepunch.com/showthread.php?t=1492234&p=49039374&viewfull=1#post49039374

The get parameters are just there to show what the api is wating for the value of steamId,
This is just an hint.

Thanks for the advice

The code of @andreblue solve this problem.

Thanks a lot guys.

Best Regards,
MadeByHost