let result = await ez.request.get(url)
ez.request.get(url) asks the server at url for its data. A server is a computer somewhere else that keeps data and hands it out, and its URL is its address, just like a web page has.
let url = 'https://tinkr.tech/sdb/citybus'
async function loadBuses() {
let result = await ez.request.get(url)
}
result is the answer. ez.request.get needs await in front of it, and await only works inside a function that has async in front of function.
Servers send their data as text in a format called JSON. It looks just like JavaScript arrays and dictionaries, and ez.request.get turns it into real ones. Open the URL in a browser tab to see that raw text.
result is a dictionary with four keys:
| Key | What it holds |
|---|---|
ok |
true if it worked, false if not |
status |
a number: 200 means fine, 0 means the server could not be reached |
body |
the data itself |
error |
null if it worked, otherwise a text that says what went wrong |
When everything worked, result looks like this:
{
ok: true,
status: 200,
body: [
{ id: '3f2a9c1e-7b4d-4e8a-9c21-5d6e7f8a9b0c', line: '5', stop: 'Market', time: '08:10' },
{ id: '8b1c4d2e-0f3a-4b5c-8d7e-1a2b3c4d5e6f', line: '17', stop: 'Harbour', time: '08:25' }
],
error: null
}
Every item has an id. The server gave it that id when the item was saved, so it can tell items apart.
Sometimes it does not work: the wifi is gone, or the URL has a typo. Then ok is false, status is 0, body is null, and error says what happened:
{
ok: false,
status: 0,
body: null,
error: 'Could not reach https://tinkr.tech/sdb/citybus. Either you are offline or the URL is wrong, ...'
}
So check result.ok before you use result.body.