18

我很难使用lua socket通用 POST。我正在尝试与身体一起发布到网络。但我得到的身体输出是 1。这是我的代码

local http = require "socket.http"
local ltn12 = require "ltn12"
local util = require "util"
    local reqbody = "anid=&protocol=1&guid=dfe49e55b63f2cf93eb9aabe44b6d9dc5286bbbedfcbf1c75b95f7a4f7439029&d_type=phone&os_version=6.1&ate=1&asid=079ABF64-A23A-4E3B-9000-19A4A608CCBE&affiliate=&modin=7c78d075f379db2f40c9f68df857cb87&os=ios&d_id=107b2734fdb7898251f62d229168484a9d14f7fb654d02d957b30c9f22bb094c&d_code=1E5D02FF-63F3-43A0-A2BF-80E63E00F76C&pn_device_id=&name_hint=iPhone%20Simulator&d_sig=dfe49e55b63f2cf93eb9aabe44b6d9dc5286bbbedfcbf1c75b95f7a4f7439029&hdid=62624a01f8715f2b838224a4a285746d&tracker=&appid=536381662&odin=1da61c680b672c4e114df45cd5f8f0aa9b088338&model=iPhone%20Simulator&ver=15&campaign=&imei=&store_type=apple&"
    local respbody = {} 
    local  body, code, headers, status = http.request {
        method = "POST",
        url = "https://freshdeck.idle-gaming.com/api/guest_session/",
        source = ltn12.source.string(reqbody),
        headers = 
                {
                        ["Accept"] = "*/*",
                        ["Accept-Encoding"] = "gzip, deflate",
                        ["Accept-Language"] = "en-us",
                        ["Content-Type"] = "application/x-www-form-urlencoded",
                        ["content-length"] = string.len(reqbody)
                },
        sink = ltn12.sink.table(respbody)
    }

    LOGINFO('body:' .. tostring(body))
    LOGINFO('code:' .. tostring(code))
    LOGINFO('headers:' .. util.tableToString(headers))
    LOGINFO('status:' .. tostring(status))

下面是输出

body:1
code:200
headers:  "set-cookie": "config_version=887; expires=Sat, 29-Jun-2013 19:07:09 GMT; Max-Age=86400; Path=/"
  "date": "Fri, 28 Jun 2013 19:07:09 GMT"
  "ed-config-version": "887"
  "content-encoding": "gzip"
  "cache-control": "private, no-cache, no-store, must-revalidate"
  "connection": "Close"
  "vary": "Cookie"
  "content-length": "52"
  "pragma": "no-cache"
  "content-type": "application/json; charset=utf-8"
  "server": "nginx/1.2.7"

status:HTTP/1.1 200 OK

我不知道为什么 body返回 1,有什么想法吗?

预先感谢您的帮助。

4

1 回答 1

16

http.request有两种形式,简单形式使用 GET 或 POST 方法下载 URL,并且基于字符串。

http.request(url [, body])

通用形式执行任何 HTTP 方法并且基于 LTN12。

http.request{
  url = string,
  [sink = LTN12 sink,]
  [method = string,]
  [headers = header-table,]
  [source = LTN12 source],
  [step = LTN12 pump step,]
  [proxy = string,]
  [redirect = boolean,]
  [create = function]
}

您使用的是通用形式,根据文档,第一个返回值应该是 1。

在失败的情况下,该函数返回 nil 后跟一条错误消息。如果成功,简单表单会以字符串形式返回响应正文,后跟响应状态代码、响应标头和响应状态行。通用函数返回相同的信息,除了第一个返回值只是数字 1(主体进入接收器)。

于 2013-06-29T00:34:24.187 回答