This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

creating POST endpoint with request body
#1
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 ?


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))
?>
Reply
#2
See this: https://forum.logicmachine.net/showthrea...6#pid22426
Reply
#3
Wow, it worked !! Thank you Admin for your help Smile
Reply


Forum Jump: