lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi Team,

I am new to LUA programming language, I am trying to call external service https call from LUA and got success, but the same thing is not working when integrating with HAProxy.Could you please help us providing sample Rest POST request calls from LUA Script using HAProxy compatible SOCKET functions.
Here I am attaching my LUA script and let us know is am missing anything?

core.Debug("actionlua called")
function check_ip(txn, addr,port)
core.Debug("actionlua called")
if not addr then addr ='3.226.158.233' end
    if not port then port = 443 end
    -- Set up a request to the service
    local payload = [[{"email":"evtest@yopmail.com"}]]

    local hdrs = {
        [1] = string.format('host: %s:%s', addr,port),
        [2] = 'accept: application/json',
        [3] = 'Content-Type: application/json'
    }

    local req = {
        [1] = 'POST /evl/evIDLookU HTTP/1.1',
        [2] = payload,
        [3] = table.concat(hdrs, '\r\n'),
        [4] = '\r\n'
    }

    req = table.concat(req,  '\r\n')
    print("req::",req)
    -- Use core.tcp to get an instance of the Socket class
    local socket = core.tcp()

    -- Connect to the service and send the request

print("address::",addr)
print("port::",port)
 if socket:connect(addr,port) then
     if socket:send(req) then
  if socket:send(req) then
        -- Get response body, if any
           local content = socket:receive("*a")
         print("content::",content)
       end
      socket:close()
    else
      core.Alert('Could not connect to IP Checker server (connect)')
    end
    -- The request should be blocked
    --txn:set_var('req.blocked', true)
end
core.register_action('checkip', {'http-req'}, check_ip)



Thanks,
B.Murali

--







B.Murali Mohan Reddy