Posts: 5
Threads: 2
Joined: Feb 2023
Reputation:
0
06.02.2023, 10:59
(This post was last modified: 06.02.2023, 11:02 by ks04.)
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
(Note, I also tried running LUA 4.1 locally and got the same result as running the later version)
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
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)
Posts: 5
Threads: 2
Joined: Feb 2023
Reputation:
0
(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}}
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
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)
Posts: 5
Threads: 2
Joined: Feb 2023
Reputation:
0
(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}
Posts: 7758
Threads: 42
Joined: Jun 2015
Reputation:
447
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).
Posts: 5
Threads: 2
Joined: Feb 2023
Reputation:
0
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!
|