Logic Machine Forum
creating POST endpoint with request body - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: creating POST endpoint with request body (/showthread.php?tid=4089)



creating POST endpoint with request body - Hadeel - 12.06.2022

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))
?>



RE: creating POST endpoint with request body - admin - 13.06.2022

See this: https://forum.logicmachine.net/showthread.php?tid=3470&pid=22426#pid22426


RE: creating POST endpoint with request body - Hadeel - 14.06.2022

Wow, it worked !! Thank you Admin for your help Smile