lua socket POST

时间:2013-06-28 19:34:10

标签: sockets http post lua

我很难使用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

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

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:13)

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

您使用的是通用表单,根据document,第一个返回值应为1。

  

如果失败,该函数返回nil,后跟一条错误消息。如果成功,则简单表单将响应正文作为字符串返回,然后是响应状态代码,响应标头和响应状态行。泛型函数返回相同的信息,除了第一个返回值只是数字1(正文进入接收器)。