通过lua脚本访问restapi

通过lua脚本访问restapi,api,post,lua,get,response,Api,Post,Lua,Get,Response,有没有办法用纯lua脚本访问restapi 获取/发布双向需要访问和显示响应 我已经试过了 local api = nil local function iniit() if api == nil then -- body api = require("http://api.com") .create() .on_get(function () return {name

有没有办法用纯lua脚本访问restapi

获取/发布双向需要访问和显示响应

我已经试过了

    local api = nil
    local function iniit()
    if api == nil then
      -- body
      api = require("http://api.com")
            .create()
            .on_get(function ()
                return {name = "Apple",
                        id = 12345}

            end)
        end
     end

在linux、mac中,我们可以轻松安装Luarock,然后安装curl包。这是实现类unix操作系统的最简单方法


-- HTTP Get
local curl = require('curl')

curl.easy{
  url = 'api.xyz.net?a=data',
  httpheader = {
    "X-Test-Header1: Header-Data1",
    "X-Test-Header2: Header-Data2",
  },
  writefunction = io.stderr -- use io.stderr:write()
}
:perform()
:close()

在windows中,我遇到了几个问题。无法正确安装凸耳。然后luarock安装命令无法正常工作,等等

在第一个dwnload中,从官方网站加载lua,然后创建类似(网站下方)的结构

然后我下载lua luadist

然后我得到了相同的结构luadist提取文件夹和lua文件夹

合并了luadist文件夹和lua文件夹 最后我们可以使用http.soket

local http=require("socket.http");

local request_body = [[login=user&password=123]]
local response_body = {}

local res, code, response_headers = http.request{
    url = "api.xyz.net?a=data",
    method = "GET", 
    headers = 
      {
          ["Content-Type"] = "application/x-www-form-urlencoded";
          ["Content-Length"] = #request_body;
      },
      source = ltn12.source.string(request_body),
      sink = ltn12.sink.table(response_body),
}

print(res)
print(code)

if type(response_headers) == "table" then
  for k, v in pairs(response_headers) do 
    print(k, v)
  end
end

print("Response body:")
if type(response_body) == "table" then
  print(table.concat(response_body))
else
  print("Not a table:", type(response_body))
end


如果您正确执行这些步骤,这将工作1000%确定

您使用的是什么Lua库?在那里我可以阅读
上的文档。在\u get
上?@EgorSkriptunoff,我完全不了解lua,所以不知道。在\u get上是不是一个图书馆。有人可以提供帮助吗?如果RESTAPI是在OpenAPI/Swagger中记录的,那么您可能希望尝试使用OpenAPI生成器来生成LuaAPI客户端:请参阅。