How to Request a Resource from a REST API
The test stack
For this lesson we will request JSON data and covert it to a LiveCode Array that we will display in a Tree View widget.
The stack consists of
- button "Get Data"
- field "JSON"
- widget "Tree View"
- button "Clear"
Requesting the data
We will request the /todo resource from the API.
mouseUp handler to the "Get Data" button and the
requestData command to the Card Script.
local tURL, tJSON, tArray
put "https://jsonplaceholder.typicode.com/todos/" into tURL
put url tURL into tJSON
put jsonToArray(tJSON) into tArray
put tJSON into field "JSON"
set the arrayData of widget "Tree View" to tArray
- Use the URL keyword to request the resource from the API
- Convert the returned JSON to an array
- Display the JSON in the field
- Display the array in the widget
The stack uses
jsonToArray which is implemented in the mergJSON library. This library is not automatically included by the Standalone Builder so ensure you have choosen "Select inclusions" in the General pane of the Standalone Application Settings and selected the Tree View widget and mergJSON library in the Inclusions pane.
On iOS and Android 9+ access to http connections is prohibited by default. If you need to use http and cannot use https there are way to allow it.
On iOS there is a "Disable ATS" setting in the iOS Standalone Application Settings.
For Android there are workarounds suggested in the report in the Quality Control Center.
This is a very simple example, for other methods you can use get, put, post and delete URL. You can also use tsNet commands and functions.