REST API communication through LUA code

Hi, so I’m experimenting on Parse’s REST API. According to the documentation, this GET method will retrieve data from the specified class. By the way, develephant has a library for Parse and it’s awesome, thank you for that develephant. I just want to learn this.

curl -X GET \ -H "X-Parse-Application-Id: ParseAppID" \ -H "X-Parse-REST-API-Key: RESTAPIKey" \ https://api.parse.com/1/classes/GameScore

I have successfully coded this with LUA and it works fine. This is my LUA code translation to the code above.

local headers = {} headers["Content-Type"] = "application/json" headers["X-Parse-Application-Id"] = ParseAppID headers["X-Parse-REST-API-Key"] = RESTAPIKey local body = "" local params = {} params.headers = headers params.body = body network.request("https://api.parse.com/1/classes/GameScore", "GET", networkListener, params)

Now I want to query data with specific conditions. Parse’s documentation has this GET method.

curl -X GET \ -H "X-Parse-Application-Id: ParseAppID" \ -H "X-Parse-REST-API-Key: RESTAPIKey" \ -G \ --data-urlencode 'where={"playerName":"Sean Plott","cheatMode":false}' \ https://api.parse.com/1/classes/GameScore

How do I translate this to LUA code? I have this code so far but it does not work. This is really a noob question. I am sorry for this. What does -G and --data mean? -H stands for Headers if I’m not mistaken.

local headers = {} headers["Content-Type"] = "application/json" headers["X-Parse-Application-Id"] = ParseAppID headers["X-Parse-REST-API-Key"] = RESTAPIKey local body = urlencode("where={playerName:Sean Plott,cheatMode:false}") local params = {} params.headers = headers params.body = body network.request("https://api.parse.com/1/classes/GameScore", "GET", networkListener, params)

I’d appreciate some guidance on this one, even just a bit :slight_smile: Thank you very much.

To whoever wanting to know the answer, I got it. I just changed the URL to

https://api.parse.com/1/classes/GameScore?where%3D%7B%22playerName%22%3A%22Sean%20Plott%22%2C%22cheatMode%22%3Afalse%7D

This is working now.

function urlencode( str )     if (str) then         str = string.gsub (str, "\n", "\r\n")         str = string.gsub (str, "([^%w])",             function (c) return string.format ("%%%02X", string.byte(c)) end)         str = string.gsub (str, " ", "+")     end     return str     end

This is a basic URL encoding function in Lua.  You technically don’t need to encode the where= part, just the code after it. You could have done:

local body = “where=” … urlencode("{playerName:Sean Plott,cheatMode:false}")

You will find this a little more useful when you have to start substituting strings for the playerName and cheatMode values.

Rob

Hi Sir, yeah, actually I did what you just posted but then I thought I’d give the exact string that should be submitted. Anyhow, thank you for this. Your tutorials are always top-notch everybody can understand. Thank you.

To whoever wanting to know the answer, I got it. I just changed the URL to

https://api.parse.com/1/classes/GameScore?where%3D%7B%22playerName%22%3A%22Sean%20Plott%22%2C%22cheatMode%22%3Afalse%7D

This is working now.

function urlencode( str )     if (str) then         str = string.gsub (str, "\n", "\r\n")         str = string.gsub (str, "([^%w])",             function (c) return string.format ("%%%02X", string.byte(c)) end)         str = string.gsub (str, " ", "+")     end     return str     end

This is a basic URL encoding function in Lua.  You technically don’t need to encode the where= part, just the code after it. You could have done:

local body = “where=” … urlencode("{playerName:Sean Plott,cheatMode:false}")

You will find this a little more useful when you have to start substituting strings for the playerName and cheatMode values.

Rob

Hi Sir, yeah, actually I did what you just posted but then I thought I’d give the exact string that should be submitted. Anyhow, thank you for this. Your tutorials are always top-notch everybody can understand. Thank you.