Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

REST API communication through LUA code
Started by chris.techno25 Oct 25 2014 07:06 PM

- - - - -
3 replies to this topic
rest api rest api network network.request

Best Answer Rob Miracle , 25 October 2014 - 07:44 PM

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

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

chris.techno25

[GLOBAL: userInfoPane.html]
chris.techno25
  • Enthusiast

  • 30 posts
  • Corona SDK

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 :) Thank you very much.



[TOPIC: post.html]
#2

chris.techno25

[GLOBAL: userInfoPane.html]
chris.techno25
  • Enthusiast

  • 30 posts
  • Corona SDK

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.



[TOPIC: post.html]
#3

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,791 posts
  • Enterprise

  Best Answer

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



[TOPIC: post.html]
#4

chris.techno25

[GLOBAL: userInfoPane.html]
chris.techno25
  • Enthusiast

  • 30 posts
  • Corona SDK

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.




[topic_controls]
[/topic_controls]