How to Request a Resource from a REST API
A RESTful Web Service is a Web Service that conforms to the REST architectural style. You can read more at https://restfulapi.net/.
This lesson will show you how to use REST API to request a resource using the
In this lesson we are using the JSONPlaceholder 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.
on mouseUp requestData end mouseUp
command requestData 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 end requestData
- 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.
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.