Logic Machine Forum
LM as REST server - 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: LM as REST server (/showthread.php?tid=4574)



LM as REST server - Christian_EWW - 09.02.2023

Hello.
I have to communicate with an external point of sales terminal. The main communication is done via REST, that’s the easy part and is working already. My problem is, that the terminal uses a callback function to inform the LM when a button is pressed by the user. So I have to implement a Server that listens for incoming data. The data is in HTML and looks like that:
 
POST / HTTP/1.1(
Accept: text/plain, application/json, application/*+json, */*(
Content-Type: application/json(
Authorization: CPS apikey="xxxxxxxx"(
User-Agent: Java/11.0.14.1(
Host: xxx.xxx.xxx.xxx:xxx(
Connection: keep-alive(
Content-Length: 71(
(
{"serialNumber":"xxxx","screenId":"Screen-1","response":"ok"}
 
I have tried to use the following code:
Code:
local socket = require("socket")
local server = assert(socket.bind("*", 4001))
local header
while 1 do
  local client = server:accept()
  local line = client:receive()
  header = line
  while (#line > 0) do
    line = client:receive()
    header = header .. line
  end
  log(header) 
end

 
But so I can only receive the header. I think that I have to use also ltn12 to get the body data, but I have no idea how. I also tried to use copas but this was also not successful.
I would be quite thankful for some help.
 
Thanks,
Christian


RE: LM as REST server - admin - 09.02.2023

If you can specify a custom URL (not just http://IP/) then you can use server-side .lp scripting.
See this for more info: https://forum.logicmachine.net/showthread.php?tid=4483&pid=28956#pid28956
Code:
<?

require('apps')
data = ngx.req.get_body_data()

if data then
  data = json.pdecode(data)
  log(data)
end



RE: LM as REST server - Christian_EWW - 09.02.2023

Thanks.
That is working, but not really my prefered way of doing it.
The cons are:
#I have to switch off user authentication
#Can't use an other port
#Harder to read and maintain code
Is there no other way?
Thanks


RE: LM as REST server - admin - 09.02.2023

You can always write your own HTTP server using scripts if you want Smile
Check if you can put username and password in the URL. If this does not work you can use public directory instead of user. It does not have any authentication checks.