Logic Machine Forum
GET request - 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: GET request (/showthread.php?tid=4696)



GET request - a455115 - 03.04.2023

Hello colleagues!

Sorry if I'm repeating a topic, but I didn't find what I needed in the search engine.

I need to submit GET request with Bearer authentication token and then get the values which are in JSON format. The values are times and dates that I need to store and compare until the time comes and executes a scene - what is the best practice for this? And how to send the request with token?


RE: GET request - admin - 03.04.2023

Use this to get JSON data, change token and url as needed:
Code:
json = require('json')
ltn12 = require('ltn12')
http = require('socket.http')

token = 'ABCDEF'
url = 'https://httpbin.org/get'

resp = {}

res, code = http.request({
  url = url,
  method = 'GET',
  headers = {
    authorization = 'Bearer ' .. token,
  },
  sink = ltn12.sink.table(resp),
})

resp = table.concat(resp)
resp = json.pdecode(resp)

log(resp)

You can store time/value pairs in storage and use a scheduled script that checks if any event should run once a minute.


RE: GET request - a455115 - 03.04.2023

Works great! Thanks admin!

 How do I store the data in the storage?


RE: GET request - admin - 03.04.2023

https://openrb.com/docs/lua.htm#storage.set


RE: GET request - a455115 - 04.04.2023

(03.04.2023, 11:18)admin Wrote: https://openrb.com/docs/lua.htm#storage.set

Thank you very much admin. As always everything works.
Since I have the end keyword in the table and lua recognizes it as end - log(resp.value[1].end.dateTime), how do I change it to work?


RE: GET request - admin - 04.04.2023

Use ['end']


RE: GET request - a455115 - 04.04.2023

(04.04.2023, 06:29)admin Wrote: Use ['end']
Where am I wrong?


log(resp.value[1].['end'].dateTime)

Lua syntax error at line 26: '<name>' expected near '['


RE: GET request - admin - 04.04.2023

Dot is not needed when using square brackets:
Code:
log(resp.value[1]['end'].dateTime)



RE: GET request - a455115 - 04.04.2023

(04.04.2023, 09:39)admin Wrote: Dot is not needed when using square brackets:
Code:
log(resp.value[1]['end'].dateTime)

i am very stupid..

Thanks again!