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 URL keyword.

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.

Add a mouseUp handler to the "Get Data" button and the requestData command to the Card Script.

on mouseUp
   requestData
end mouseUp
Click to copy
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
Click to copy
  • 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.

https://quality.livecode.com/show_bug.cgi?id=22400

Further Options

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.

2 Comments

terii

"...On iOS and Android 9+ access to http connections is prohibited by default."

is this only true for http...or also for https type connections ?
Thanks

Elanor Buchanan

Hi terii

Only http connection are prohibited by default, https connections should be fine.

I hope that helps.

Elanor

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.