【问题标题】:Send request with body with openresty lua-resty-http module使用 openresty lua-resty-http 模块发送带有正文的请求
【发布时间】:2015-09-28 05:36:28
【问题描述】:

我正在尝试通过 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 表位置。

希望能有详细的解释。

【问题讨论】:

    标签: nginx lua openresty


    【解决方案1】:

    ngx.req.get_post_args() 返回一个键值对表。 http 客户端的request 函数的主体参数必须在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),
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 2021-06-06
      • 2020-01-27
      • 2023-02-19
      • 2019-03-15
      • 1970-01-01
      • 1970-01-01
      • 2018-01-31
      • 1970-01-01
      • 2014-07-06
      相关资源
      最近更新 更多