Can't seem to figure out xml2lua usage
mnapoleonv opened this issue · 1 comments
I'm attempting to parse an XML coming in via a POST call in nginx with. I created a simple lua script that ngnix will use
local xml2lua = require("xml2lua")
local handler = require("xmlhandler.tree")
local function process()
ngx.req.read_body()
local req_body = ngx.req.get_body_data()
ngx.log(ngx.STDERR, "Type of body " .. type(req_body))
ngx.log(ngx.STDERR, "the body" .. req_body)
local thehandler = handler:new()
local theparser = xml2lua.parser(thehandler)
theparser:parse(req_body)
ngx.say(xml2lua.printable(thehandler.root))
for i, p in pairs(thehandler.root.top) do
-- print("table size " .. table.maxn(p))
ngx.say(i .. " Test " .. p.inner)
end
end
return process
Then I post some simple xml
<top> <inner stuff="val1">Data</inner> <inner stuff="val2">MoreData</inner> <inner stuff="val3">SomeMoreData</inner> </top>
I can print the xml body to the nginx logs but I get this error with regards to the lua table from the xml.
10.7.1.87, server: , request: "POST / HTTP/1.1", host: "10.4.2.231:8080" 2020/08/06 17:42:20 [] 8155#8155: *4 [lua] osphandler.lua:8: the body<top> <inner stuff="val1">Data</inner> <inner stuff="val2">MoreData</inner> <inner stuff="val3">SomeMoreData</inner> </top>, client: 10.7.1.87, server: , request: "POST / HTTP/1.1", host: "10.4.2.231:8080" 2020/08/06 17:42:20 [error] 8155#8155: *4 lua entry thread aborted: runtime error: /home/ec2-user/work//src/osphandler.lua:16: attempt to index field 'inner' (a nil value) stack traceback: coroutine 0: /home/ec2-user/work//src/osphandler.lua: in function </home/ec2-user/work//src/osphandler.lua:4>
I'm new to lua so forgive any obvious issues as they aren't obvious to me.
It's probably an issue with your server response, not the library.
Firstly try to use the library with a local XML file.