Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
捕获Lua套接字调用中的完整响应_Lua_Luasocket_Luarocks - Fatal编程技术网

捕获Lua套接字调用中的完整响应

捕获Lua套接字调用中的完整响应,lua,luasocket,luarocks,Lua,Luasocket,Luarocks,我正试图通过LUA调用restapi。但是,我无法捕获API返回的完整原始响应。下面是代码示例: local http_socket = require("socket.http") local pretty_print = require("pl.pretty") local header = { ["x-device-type"] = "M", ["authorization"] = "ashdjkashd",

我正试图通过LUA调用restapi。但是,我无法捕获API返回的完整原始响应。下面是代码示例:

local http_socket = require("socket.http")
local pretty_print = require("pl.pretty")
local header = {
                 ["x-device-type"] = "M",
                 ["authorization"] = "ashdjkashd",
                 ["x-app-secret"] = "asdasda",
                 ["x-user-id"] = "asdasdasd"
                 }

r, c, h = http_socket.request {
       method = "GET",                          -- Validation API Method                           
       url = "http://google.com",   -- Validation API URL
       headers = header
}
print(r .. c)
pretty_print.dump(h)
我使用的是Lua5.3,luarocks版本=2.4.1。
在变量c中我得到了代码,在h中有一些头。我需要捕获API返回的完整响应。

您可能知道,luasocket的
http.request
支持。我假设您需要第二个表单来定制特定API的resty请求

在这种情况下,要捕获响应主体,需要将
sink
字段与
ltn12.sink
模块一起使用。比如说

local ltn12 = require 'ltn12'

-- ...

local res = {}
r, c, h, s = http_socket.request
{
  method = "GET",               -- Validation API Method
  url = "http://google.com",    -- Validation API URL
  headers = header,
  sink = ltn12.sink.table(res)
}

res = table.concat(res)
print(res)
需要
table.concat
,因为响应可能由多个块大小组成(在接收时附加到
res


您也可以通过将上面的内容替换为
ltn12.sink.file
将其写入文件,例如,使用
ltn12.sink.file(io.stdout)
将把响应转储到标准输出。

非常感谢!工作很有魅力。我错过了
表。concat
。谢谢