1
votes

I am trying to send request via http module lua-resty-http. How I can send request with body data.

I have tried this

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",
    },
}

Basically I am trying to send a lua table to another server location. And how to capture on that lua table location.

I'd appreciate a detailed explanation.

1

1 Answers

3
votes

ngx.req.get_post_args() returns a table of key, value pairs. The body argument for the http client's request function must be in a format supported by OpenResty's cosocket send API. This means either a string, or array like table holding strings.

If you want to send a lua table with an HTTP request then you'll need a way to encode it to a string. A common approach is using JSON, and you can do this with the bundled cjson library:

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

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