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 ...