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.

REST with JSON
#2
(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
Reply


Messages In This Thread
REST with JSON - by toujour - 05.10.2024, 08:49
RE: REST with JSON - by CristianAgata - 05.10.2024, 09:18
RE: REST with JSON - by toujour - 10.10.2024, 08:08

Forum Jump: