Hi!
Our team is trying to develop a LM application in order to use it as a POST endpoint.
In index.lp file I have a Lua script like below.
When I send a request with JSON body like {"commands":[{"alias":"0/0/2","value":1},{"alias":"0/0/5","value":1},{"alias":"0/0/10","value":1},{"alias":"0/0/11","value":1}]}
I get an error "Error at line 5: bad argument #1 to 'decode' (string expected, got table) ".
I can understand it's because getvars() returns a table, but even if I skip this line the params.commands in line 8 returns nil.
What should I do to get proper value in params.commands ?
Our team is trying to develop a LM application in order to use it as a POST endpoint.
In index.lp file I have a Lua script like below.
When I send a request with JSON body like {"commands":[{"alias":"0/0/2","value":1},{"alias":"0/0/5","value":1},{"alias":"0/0/10","value":1},{"alias":"0/0/11","value":1}]}
I get an error "Error at line 5: bad argument #1 to 'decode' (string expected, got table) ".
I can understand it's because getvars() returns a table, but even if I skip this line the params.commands in line 8 returns nil.
What should I do to get proper value in params.commands ?
Code:
<?
require('json')
require('apps')
params = getvars()
params = json.decode(params)
results = {}
for index, comamnd in ipairs(params.commands) do
if type(command.alias) ~= nil or type(command.value) ~= nil then
result = grp.write(command.alias, command.value)
results[command.alias] = result
end
end
results = {
results = results
} -- e.g. '{"results":{"0/0/2":true,"0/0/5":false}}'
write(json.encode(results))
?>