【问题标题】:equivalent CURL for Lua, (Django API Json to Lua Json) POST methodLua 的等效 CURL,(Django API Json 到 Lua Json)POST 方法
【发布时间】: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 中遇到jsonLua 中的问题。我希望返回的 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


    【解决方案1】:

    为什么不使用 Corona 提供的 network.request 功能呢?
    它也是异步的。

    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)
    

    在这里阅读 http://developer.anscamobile.com/reference/index/networkrequest

    编辑

    如果你真的想使用 http.request 那么你可以这样做。

    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)
    
    }
    

    【讨论】:

    • 感谢 SatheeshJM 的回答......在print(event.response),print(event.isError),print(event.status) 我得到了这个输出。对于event.isError = trueevent.response = '' 和 event.status = nil ?为什么我得到error
    • 好吧!我不知道。根据文档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
    • 您是否正确设置了内容长度? headers["content-length"] = body:len() 而不是 headers["content-length"] = "" 我现在更新了我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 2018-12-18
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多