Logic Machine Forum
REST with JSON - 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: REST with JSON (/showthread.php?tid=5655)



REST with JSON - toujour - 05.10.2024

Hi,

what is the better way to send some JSON values to a REST server ?

Something about this ?


https = require('ssl.https')
url = 'http://IP_MACHINE/url'
body = [[{
"timestamp": NOW(),
"meter": grp.getvalue('32/2/4')
}]]
resp = {}
res, code, hdrs, stat = https.request({
  url = url,
  method = 'POST',
  headers = {
    ['Content-Type'] = 'application/json',
    ['Content-Length'] = #body,
  },
  source = ltn12.source.string(body),
  sink = ltn12.sink.table(resp),
})
resp = table.concat(resp)
log(res, code, hdrs, stat, resp)



How can I use NOW() and grp.getvalue inside of the JSON ?


RE: REST with JSON - CristianAgata - 05.10.2024

(05.10.2024, 08:49)toujour Wrote: Hi,

what is the better way to send some JSON values to a REST server ?

Something about this ?


https = require('ssl.https')
url = 'http://IP_MACHINE/url'
body = [[{
"timestamp": NOW(),
"meter": grp.getvalue('32/2/4')
}]]
resp = {}
res, code, hdrs, stat = https.request({
  url = url,
  method = 'POST',
  headers = {
    ['Content-Type'] = 'application/json',
    ['Content-Length'] = #body,
  },
  source = ltn12.source.string(body),
  sink = ltn12.sink.table(resp),
})
resp = table.concat(resp)
log(res, code, hdrs, stat, resp)



How can I use NOW() and grp.getvalue inside of the JSON ?
Hi toujour,
try to add this:
Code:
require('json')

ora = os.time()
value = grp.getvalue('your_group_address')
body = json.encode({
  timestamp = ora,
  meter = value,
})
log (body)

best regards Cristian


RE: REST with JSON - toujour - 10.10.2024

It's ok !!