lua 套接字 POST [英] lua socket POST

查看:39
本文介绍了lua 套接字 POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a hard time using lua socket generic POST. I'm trying to POST to a web along with a body. But the body output i got is 1. Here's my code

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,有什么想法吗?

I don't know why the body returns 1, any ideas?

预先感谢您的帮助.

推荐答案

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

http.request has two forms, The simple form downloads a URL using the GET or POST method and is based on strings.

http.request(url [, body])

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

The generic form performs any HTTP method and is LTN12 based.

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.

You are using the generic form, and according to the document, the first return value is supposed to be 1.

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

In case of failure, the function returns nil followed by an error message. If successful, the simple form returns the response body as a string, followed by the response status code, the response headers and the response status line. The generic function returns the same information, except the first return value is just the number 1 (the body goes to the sink).

这篇关于lua 套接字 POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆