Parse as a backend?

Looking over the Parse documentation, it seems like one could use the backend service to store user data. However, the examples show CURL, which I don’t think is supported in Corona. Is there a way to authenticate using JSON?

https://www.parse.com/docs/rest
[import]uid: 1560 topic_id: 15637 reply_id: 315637[/import]

I’m also wondering if I can use parse with corona.
Did you figure out a way to do it?
[import]uid: 13632 topic_id: 15637 reply_id: 80348[/import]

You should be able to authenticate with something like this:

[code]
local json = require “json”

local function networkListener (event)
print (event.response)
end

local postData = {
[“username”] = “myusername”,
[“password”] = “mypassword”,
[“phone”] = “000000000”
}
local jsonpostData = json.encode (postData)
local params = {}
params.body = jsonpostData

local url = “https://api.parse.com/1/users
network.request( url, “POST”, networkListener, params)
[/code] [import]uid: 10141 topic_id: 15637 reply_id: 80351[/import]

Parse looks like a really cool service, but their REST protocols used HTTP PUT and DELETE which are currently not supported my Corona SDK’s network.request API.

So you can only use their service in a limited way.
[import]uid: 19626 topic_id: 15637 reply_id: 80362[/import]

I think that DELETE works, or used to work but PUT doesn’t [import]uid: 10141 topic_id: 15637 reply_id: 80370[/import]

Read the responses under the API comments. #23 is a statement from Ansca that pretty much sums this up.

DELETE may work in the simulator, but that doesn’t mean it works on Device from things I’ve read. There is no evidence that PUT works at all.

http://developer.anscamobile.com/reference/index/networkrequest
[import]uid: 19626 topic_id: 15637 reply_id: 80374[/import]

Thanks for the info robmiracle [import]uid: 10141 topic_id: 15637 reply_id: 80377[/import]

Thanks to both of you. [import]uid: 13632 topic_id: 15637 reply_id: 80379[/import]

According to the Urban Airship thread PUT is working on iOS in current builds. [import]uid: 110373 topic_id: 15637 reply_id: 80381[/import]

Can anyone confirm that PUT is working on device builds? [import]uid: 13632 topic_id: 15637 reply_id: 80463[/import]

Good news… it is possible to use parse with corona!

I tested PUT and DELETE with parse and it works as it is supposed to both in simulator and on device. I used daily build 709. [import]uid: 13632 topic_id: 15637 reply_id: 81024[/import]

and by the way… I only tested it on iOS,
with the parse REST API
[import]uid: 13632 topic_id: 15637 reply_id: 81026[/import]

Ojnab, would you be wiling to share your code? [import]uid: 110373 topic_id: 15637 reply_id: 81107[/import]

Hi Brad

Yes here it is.
You will have to create an account and a test application at parse.com, after you have done that you can find the “Application-Id” and the “REST-API-Key” in your dashboard there.
Copy the id and the key into the code below and you are ready to go.

I have been trying to upload and download images to the parse backend, but have some problems decoding the image file. If anyone can help me with this I will be very happy. See this link:

https://developer.anscamobile.com/forum/2012/01/20/downloading-base64-encoded-image-server

Here is the code:

[lua]local json = require “json”

local baseUrl = “https://api.parse.com/1/classes/
local objectClass = “testClass”
local objectId = nil

local responseTF

local headers = {}

headers[“Content-Type”] = “application/json”
headers[“X-Parse-Application-Id”] = “” – your Application-Id
headers[“X-Parse-REST-API-Key”] = “” – your REST-API-Key

local params = {}
params.headers = headers

local function networkListener( event )

if ( event.isError ) then
print( “Network error!”)
else
local response = json.decode ( event.response )

if response[“objectId”] then
objectId = response[“objectId”]
end
end
responseTF.text = "RESPONSE: " … event.response
end

local function post ( message )
params.body = json.encode ( message )
network.request( baseUrl … objectClass, “POST”, networkListener, params)
end

local function put ( message, id )
if id then
params.body = json.encode ( message )
network.request( baseUrl … objectClass …"/"… id, “PUT”, networkListener, params)
end
end

local function delete ( id )
if id then network.request( baseUrl … objectClass …"/"… id, “DELETE”, networkListener, params) end
objectId = nil
end

local function get ( id )
params.body = nil
if id then network.request( baseUrl … objectClass …"/"… id, “GET”, networkListener, params) end
end

– set up buttons
local message = { foo = “bar” }
responseTF = display.newText ( “”, 30, 260, 260, 200, native.systemFont, 12)

local function buttonHandler ( event )

local method = event.target.method

if event.phase == “began” then

if method == “POST” then post ( message )
elseif method == “GET” then get ( objectId )
elseif method == “PUT” then put ( message, objectId )
elseif method == “DELETE” then delete ( objectId ) end
end
end

local buttons = { “POST”, “GET”, “PUT”, “DELETE” }

for i=1,#buttons do
local b = display.newText ( buttons[i], 30, 50*i, native.systemFontBold, 23)
b.method = buttons[i]
b:addEventListener ( “touch”, buttonHandler )
end[/lua]
[import]uid: 13632 topic_id: 15637 reply_id: 81554[/import]

@ojnab Thanks for this useful exemple! [import]uid: 25327 topic_id: 15637 reply_id: 91046[/import]

@Michael - You are welcome.
BTW parse also added file uploading to the REST API,
so now it is easy to upload images etc. [import]uid: 13632 topic_id: 15637 reply_id: 91061[/import]

Hey Ojnab,

I’ve built on the code you posted and have a fairly function Parse interface. Mind if I post it in the code library? [import]uid: 1560 topic_id: 15637 reply_id: 100443[/import]

No not at all.
Looking forward to see what you’ve come up with :slight_smile:

BTW I had some email correspondence with someone from the parse crew and he told me they were considering doing a sdk for corona. [import]uid: 13632 topic_id: 15637 reply_id: 100509[/import]

If we can use Parse’s REST API from Corona, is the same true for Stackmob’s REST API? [import]uid: 135391 topic_id: 15637 reply_id: 100899[/import]

I’ve posted a more extensive Parse module on Github. It’s not quite plug-and-play, but you should be able to added it to an existing project without too much trouble. Just run main.lua in the Corona simulator and it should be pretty self explanatory. The code is documented here and there too.

http://developer.anscamobile.com/code/parse-corona-sdk
[import]uid: 1560 topic_id: 15637 reply_id: 101138[/import]