使用openresty lua-resty-http模块发送请求

时间:2015-07-09 17:27:28

标签: nginx lua openresty

我正在尝试通过http模块lua-resty-http发送请求。如何使用正文数据发送请求。

我试过这个

hc:connect("127.0.0.1", 82)

dates = ngx.req.get_post_args()

local hc = http:new()

result, errors = hc:request{
    path = requrl,
    method = "POST",
    body = dates,
    headers = {
      ["Host"] = "localhost",
    },
}

基本上我正在尝试将lua表发送到另一个服务器位置。以及如何在lua表位置捕获。

我很欣赏详细解释。

1 个答案:

答案 0 :(得分:1)

ngx.req.get_post_args()返回一个键值对表。 http客户端的request函数的body参数必须位于format supported by OpenResty's cosocket send API中。这意味着字符串或数组,如表持字符串。

如果要发送带有HTTP请求的lua表,则需要一种方法将其编码为字符串。一种常见的方法是使用JSON,您可以使用捆绑的cjson库执行此操作:

local json = require "cjson"
local dates = ngx.req.get_post_args()

hc:request {
  body = json.encode(dates),
  ...
}