【发布时间】:2012-05-29 04:00:55
【问题描述】:
我有一个具有 JSON API 的 Django,我希望它能够在我的 Lua (Corona SDK) 项目中使用它。
如果我 CURL 我的 Django 项目。
curl -l -X POST -d "message=getstrings" http://127.0.0.1:8000/api/getstrings/
这将返回:
{
"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
}
我在post method 中遇到json 在Lua 中的问题。我希望返回的 json 将在 Lua 中获取。
我在我的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)
当我打开r.txt:
我明白了……
File "home/myhome/workspace/djangoproj/api/handlers.py", line 21, in create
if attrs['message'] == 'getstrings':
KeyError: 'message'
我知道错误的原因是什么,因为 message 及其值没有被 Lua 传递。
我的问题是,像这样 CURL 的等效代码是什么
curl -l -X POST -d "message=getstrings" http://127.0.0.1:8000/api/getstrings/
在 Lua (Corona SDK) 中以便 Lua 可以获取并下载返回的Json?我的Lua 中的代码是否正确?
有人知道我的案子吗? 在此先感谢...
【问题讨论】:
标签: json curl lua coronasdk django-piston