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.

GET request
#1
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?
Reply
#2
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.
Reply
#3
Works great! Thanks admin!

 How do I store the data in the storage?
Reply
#4
https://openrb.com/docs/lua.htm#storage.set
Reply
#5
(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?
Reply
#6
Use ['end']
Reply
#7
(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 '['
Reply
#8
Dot is not needed when using square brackets:
Code:
log(resp.value[1]['end'].dateTime)
Reply
#9
(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!
Reply


Forum Jump: