Logic Machine Forum
POST API Request not Identical to running locally - 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: POST API Request not Identical to running locally (/showthread.php?tid=4558)



POST API Request not Identical to running locally - ks04 - 06.02.2023

Hi There,

I have an iZone AC unit which I can query off API POST commands however am having issues getting the same result from the LM device (SHAC). 

I am able to send POST commands fine via postman and receive the expected result, and installed LUA on my mac and was able to get the expected result, however, I am getting an error response back from the target device.

Has anyone encountered anything similar?  Does the LM device add any additional gumph or formatting into the POST request which might be throwing this off?  Open to all ideas

[Image: upload_2023-1-29_11-0-13-png.3091]

(Note, I also tried running LUA 4.1 locally and got the same result as running the later version)


RE: POST API Request not Identical to running locally - admin - 06.02.2023

Can you post the raw request that is sent from Postman? Some devices can't understand certain headers or headers must be in a specific case. LM use canonical header format.
Lua version does not matter, but check which LuaSocket version you have:
Code:
print(require('socket')._VERSION)



RE: POST API Request not Identical to running locally - ks04 - 06.02.2023

(06.02.2023, 11:59)admin Wrote: Can you post the raw request that is sent from Postman? Some devices can't understand certain headers or headers must be in a specific case. LM use canonical header format.
Lua version does not matter, but check which LuaSocket version you have:
Code:
print(require('socket')._VERSION)

LuaSocket version on LM is 2.0.2 vs locally is 3.0.0 so this could be contributing to the difference in experience.

Here's the raw postman request:
Code:
{"iZoneV2Request": {"Type": 1,"No": 0,"No1": 0}}



RE: POST API Request not Identical to running locally - admin - 07.02.2023

Try this:
Code:
http = require('socket.http')
ltn12 = require('ltn12')

body = '{"iZoneV2Request": {"Type": 1,"No": 0,"No1": 0}}'
resp = {}

url = 'http://...'

res, code = http.request({
  url = url,
  method = 'POST',
  headers = {
    ['Content-Type']  = 'application/json',
    ['Content-Length'] = #body,
  },
  source = ltn12.source.string(body),
  sink = ltn12.sink.table(resp),
})

log(res, code, resp)



RE: POST API Request not Identical to running locally - ks04 - 08.02.2023

(07.02.2023, 08:02)admin Wrote: Try this:
Code:
http = require('socket.http')
ltn12 = require('ltn12')

body = '{"iZoneV2Request": {"Type": 1,"No": 0,"No1": 0}}'
resp = {}

url = 'http://...'

res, code = http.request({
  url = url,
  method = 'POST',
  headers = {
    ['Content-Type']  = 'application/json',
    ['Content-Length'] = #body,
  },
  source = ltn12.source.string(body),
  sink = ltn12.sink.table(resp),
})

log(res, code, resp)

Sadly, same error

Code:
Event for 0/56/180 08.02.2023 11:24:21
* table:
[1]
  * string: {ERROR}



RE: POST API Request not Identical to running locally - admin - 08.02.2023

One more thing to try is to send a request using raw sockets. Modify host, uri and body as needed.
Code:
host = '192.168.0.9'
uri = '/public/test.lp'
body = 'a=b&c=d'

sock = require('socket').tcp()
sock:settimeout(5)

res, err = sock:connect(host, 80)

if res then
  crlf = '\r\n'
  sock:send(
    'POST ' .. uri .. ' HTTP/1.1' .. crlf ..
    'host: ' .. host .. crlf ..
    'connection: close' .. crlf ..
    'content-length: ' .. #body .. crlf .. crlf ..
    body
  )

  res, err = sock:receive('*a')
  sock:close()

  log(res, err)
else
  log('connect error', err)
end
If it still does not work then check the whole raw request in Postman (starting from POST .. HTTP/1.1).


RE: POST API Request not Identical to running locally - ks04 - 08.02.2023

Success! Thanks so much - that's been bugging me for ages now!

Would love to know why the original approach doesn't work, but will take the win!