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.

POST API Request not Identical to running locally
#1
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)
Reply
#2
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)
Reply
#3
(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}}
Reply
#4
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)
Reply
#5
(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}
Reply
#6
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).
Reply
#7
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!
Reply


Forum Jump: