1

I have a Django that having API for JSON, and I want it to get it in my Lua (Corona SDK) project.

if I CURL my Django project.

curl -l -X POST -d "message=getstrings" http://127.0.0.1:8000/api/getstrings/

this will return:

{
    "message": "Something good happened on the server!", 
    "data": [
        {
            "code": "003", 
            "doc1": "sd.doc", 
            "title": "Test", 
            "artist": "ABBA", 
            "img": "sd.png", 
            "genre": "Pop"
        }, 
        {
            "code": "004", 
            "doc1": "sdsd.doc", 
            "title": "sdf", 
            "artist": "ABBA", 
            "img": "sdsd.png", 
            "genre": "Pop"
        }
    ], 
    "success": true
}

i have a problem in post method for json in Lua. i want the returned json will be get in Lua.

i try this in my Lua.

local response = {}
local r, c, h = http.request{
  url= "http://127.0.0.1:8000/api/getstrings/",
  method = "POST",
  headers = {
    ["content-length"] = "",
    ["Content-Type"] = "application/x-www-form-urlencoded"
  },
  source = ltn12.source.string(post),
  sink = ltn12.sink.table(response)

}
local path = system.pathForFile("r.txt", system.DocumentsDirectory)
local file = io.open (path, "w")

file:write (response[1] .. "\n")
io.close (file)

when i open r.txt:

i got this ...

File "home/myhome/workspace/djangoproj/api/handlers.py", line 21, in create
  if attrs['message'] == 'getstrings':

KeyError: 'message'

i know what cause of the error because message and its value did not passed by Lua. my question is that what is the equivalent code like this CURL

curl -l -X POST -d "message=getstrings" http://127.0.0.1:8000/api/getstrings/

in Lua (Corona SDK) so that the Lua can get and download the returned Json? is my code in my Lua is correct?

do anyone have an idea about my case? thanks in advance ...

gadss
  • 21,687
  • 41
  • 104
  • 154

1 Answers1

1

Why not use the network.request function provided by Corona?
It is asynchronous as well.

local function listener(event)
    print(event.response)
    print(event.isError)
    print(event.status)
end


local url = "http://127.0.0.1:8000/api/getstrings/"

local body = "message=getstrings"

local headers = {}
headers["content-length"] = body:len(),
headers["Content-Type"] = "application/x-www-form-urlencoded"



local postData = {}
postData.body = body
postData.headers = headers

network.request(url,"POST",listener,postData)

Have a read here http://developer.anscamobile.com/reference/index/networkrequest

EDIT

If you REALLY want to use http.request then you can do this.

local url = "http://127.0.0.1:8000/api/getstrings/"
local body = "message=getstrings"
local headers = {
    ["content-length"] = body:len(),
    ["Content-Type"] = "application/x-www-form-urlencoded"
  }

local response = {}
local r, c, h = http.request{
  url= url,
  method = "POST",
  headers = headers,
  source = ltn12.source.string(body),
  sink = ltn12.sink.table(response)

}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
SatheeshJM
  • 3,575
  • 8
  • 37
  • 60
  • thanks SatheeshJM for the answer... in the `print(event.response),print(event.isError),print(event.status)` i got this out-put. for `event.isError` = `true`, `event.response` = '' and `event.status` = `nil` ? why did i get `error`? – gadss May 29 '12 at 05:24
  • Well! I'm not sure. According to the docs `event.isError: A boolean value: true in the case of a network error, false otherwise. As of build 2012.744, "event.isError" only returns true if we failed to connect to the server or upon response timeout` – SatheeshJM May 29 '12 at 05:29
  • Did you set the content length correctly? `headers["content-length"] = body:len()` and not `headers["content-length"] = ""` I have updated my answer now – SatheeshJM May 29 '12 at 06:12