Amir
Amir

Reputation: 1683

JSON network request not working with variables in Coronasdk

I have written the following code to send my gps locations saved in a Lua table to parse.com; the display.newText is a test to see if the Lua table data works properly which does so. The problem is that the POST network request doesn't work with my "message" table when used as the JSON parameter. Although exact what you see is working when I remove the -- from the following code. This defines the "message" table variables again. I use Corona SDK.

   function post ( message )
    test12 = display.newText( message["x1"] .. message["y1"] .. message["x2"] .. message["y2"]..message["v"], 150, time/100, "Verdana-Bold", 10 )
    --message = { x1 = 22.312456, y1 = 22.312456, x2 = 22.312456, y2 = 22.312456, v = 10 }
    params.body = json.encode ( message )
    network.request( baseUrl .. objectClass, "POST", networkListener,  params)
   end

Upvotes: 0

Views: 660

Answers (3)

Amir
Amir

Reputation: 1683

The issue was the Corona changes the variable to string while parse.com expected numbers. I simply multiplied all the table variables by 1 and then the whole things works ok.

    function post ( message )
     test12 = display.newText( message["x1"] .. message["y1"] .. message["x2"] .. message["y2"]..message["v"], 150, time/100, "Verdana-Bold", 10 )
     --message = { x1 = 22.312456, y1 = 22.312456, x2 = 22.312456, y2 = 22.312456, v = 10 }
            message["x1"] = message["x1"]*1
            message["y1"] = message["y1"]*1
            message["y2"] = message["y2"]*1
            message["x2"] = message["x2"]*1
            message["v"] = message["v"]*1
            params.body = json.encode ( message )
            network.request( baseUrl .. objectClass, "POST", networkListener,  params)
    end

If you have similar issues with Sqlite in corona that's the way to fix it.

Upvotes: 0

Rob Miracle
Rob Miracle

Reputation: 3063

I think you need to have a better understanding of what Parse is looking for. I don't know Parse well enough to tell you what it is, but HTTP POST requires a body that is a set of HTTP Query String style key-value pairs. We've seen GET use things like:

http:/somesite.com/someurl.php?key=value&key2=value&key3=value

HTTP GET is limited to like 255 characters and the values have to be URLencoded.

HTTP POST is just like GET but it allows longer blocks of data to be sent on the input stream of the device These blocks of data are still Query String key-value pairs. I would expect the body statement (though not for your usage of Parse) to be something like:

params.body = "key=value&key2=value&key3=value" where the values of course could be much longer and any values properly URL encoded. Just shoving a block of JSON data in there isn't going to be something a POST script would parse. Perhaps something like:

params.body = "query=" .. urlencode( json.encode( someLuaTable ) )

You can use this URL encoding function:

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

But you need to figure out what key Parse wants for its input.

Upvotes: 1

Dev Ze
Dev Ze

Reputation: 116

Your message has not the right format. try this:

message = {"x1":22.312456,"y1":22.312456,"x2":22.312456,"y2":22.312456,"v":10}

cheers, Felix

Upvotes: 0

Related Questions