The HTTP RESTful API

This example uses the HTTP RESTful API to interact with the device using HTTP requests. Commands are sent to the device using HTTP REST methods such as GET and POST and a simple request syntax.

Using HTTP requests, you can send any command to the device and receive the response. The Gecko OS Web App offers a full demonstration of the HTTP RESTful API. This application note demonstrates how to use the basic requests of the RESTful API.

Other ways of managing and interacting with a Gecko OS device include:

This demo uses the following Gecko OS features:


This example demonstrates how to:


Setup

To run this example follow these steps:

Connect Your Device to the Local Network

Since you'll need to configure mDNS and the web server, the quickest way to setup the device is to use a Gecko OS terminal - see Getting Started . Once you have a terminal connected, issue the following command to configure the credentials for the local network:

> network_up -s

Enable mDNS

Enable mDNS and specify the domain the device will respond to using the following commands:

set mdns.enabled 1
set mdns.name mydevice

Enable the HTTP Server

Enable the Gecko OS HTTP server and the RESTful API using the following Gecko OS commands:

set http.server.enabled     1
set http.server.api_enabled 1

Save the settings, then restart the network:

save
network_down
network_up

The device automatically starts the mDNS discovery service and the HTTP server on boot after connecting to the network.

The response from Gecko OS is similar to the following:

> Obtaining IPv4 address via DHCP
IPv4 address: 10.5.6.59
Starting mDNS
mDNS domain: mydevice.local
HTTP and REST API server listening on port: 80
[2019-01-01 | 23:48:42: Associated]

Syntax of REST API Gecko OS Commands

The format of a REST API Gecko OS command GET request is: GET http://mydevice.local/command/<command>

For example, to issue the ver command: GET http://mydevice.local/command/ver

To issue a GET request from a browser, enter the URL http://mydevice.local/command/ver into the browser URL address box.

The format of a REST API Gecko OS command POST request is: POST http://mydevice.local/command/<command> followed immediately by a json formatted structure of the form:

{
   "flags"   : <flags>,
   "command" : "<Gecko OS command>",
   "data"    : "<command data>"
}

The "data" value is required by commands that expect additional data sent immediately after the command, such as file_create . Some of the curl examples below demonstrate how to issue an HTTP POST request.

Sending Commands from a Web Browser

Using your favorite web browser (with a network client such as a PC, phone or tablet connected to the same local network as the device), enter the following URL to retrieve the Gecko OS version string, or click the following link if this computer is connected to the same network as the device:

http://mydevice.local/command/ver

The browser responds with the Gecko OS version embedded in a JSON object, similar to the following text.

{"id":18,"code":0,"flags":0,"response":"Gecko OS 4.0, Built:2019-01-15 ... }

Troubleshooting

If you are having problems making this work, try the following


How does this work?

When the Gecko OS command is entered into the web browser address box, the browser translates the URL into an HTTP GET request and sends the request to the device name. The operating system in your PC (or phone / tablet) translates the device name into the device IP address and sends the request to the device. On the device, the HTTP web server receives and processes the request, and then provides a response back to the server.

A few more examples are shown below. For full documentation on the Gecko OS HTTP REST API, see Networking and Security, HTTP server with RESTful API .

Read a list of files on the device:

URL http://mydevice.local/command/ls
Gecko OS
Response
{"id":2,"code":0,"flags":0,"response":"! # Size Version Filename\r\n# 0 41741 2.1.0 command_help.csv\r\n# 1 135 2.1.0 default_setup.script\r\n# 2 1897 2.1.0 favicon.ico.gz\r\n# 3 1236 2.1.0 geotrust_ca.pem\r\n# 4 212736 2.1.0 sys/kernel.bin\r\n# 5 215204 2.1.0 sys/services.bin\r\n# 6 38004 2.1.0 gecko_os_webgui.css.gz\r\n# 7 1827 2.1.0 gecko_os_webgui.html\r\n# 8 61492 2.1.0 gecko_os_webgui.js.gz\r\n# 9 210412 5.26.230 wifi_fw.bin\r\n"}
URL Thermistor GPIO Response
http://mydevice.local/adc 7 7
http://mydevice.local/adc 20 20

Typical response:

{"id":104,"code":0,"flags":0,"response":"0x835\r\n"}

Open a TCP connection to Google:

http://mydevice.local/command/tcp_client google.com 80

Response:

{"id":4,"code":0,"flags":0,"response":"0\r\n"}

Read the response to the TCP client connection above:

http://mydevice.local/log

Response:

{"logs":["HTTP and REST API server listening on port: 80","[2019-01-15 - 01:09:23: Associated]\r\n> ","Resolving host: google.com","[2019-01-15 - 01:28:53: Opening: google.com:80]","Connecting (TCP): 216.58.220.142:80","[2019-01-15 - 01:28:54: Opened: 0]"]}

Sending Commands Using JavaScript XMLHttpRequest

This demonstration uses POST requests. It is possible to send GET requests using XMLHttpRequest , but POST is more flexible. POST has no size restrictions, and the JSON data attribute allows additional data to be sent for Gecko OS commands such as file_create .

Download the demonstration HTML file js_rest_post.html .

This can be run directly as a file URL, provided you enable CORS for all domains on your device.

The following Gecko OS commands set up the device to work with the demonstration HTML page:

set wlan.ssid               <YOUR_SSID>
set wlan.passkey            <YOUR_PASSWORD>
set wlan.auto_join.enabled  1
set http.server.enabled     1
set mdns.name               mydevice
set mdns.enabled            1
set http.server.cors_origin *
save
reboot

Run the HTML file in a browser with the JavaScript developed console displayed. This shows errors that cannot be caught and displayed with the JavaScript try and catch mechanism.

You can run any Gecko OS command from this HTML. Just edit the request fields and click the Send POST Request button.

The demonstration JavaScript assembles the POST JSON from the request fields and makes an XMLHttpRequest to send the command to the device and display the response.

View the HTML source to see the JavaScript. The core of the demonstration is the sendPost() function.

For higher level access to the HTTP RESTful API via JavaScript, see Gecko OS JavaScript API .

Sending Commands Using cURL

Curl , a command line URL utility, may also be used to connect to the Gecko OS webserver. Curl is typically run from a PC or Linux command line for transferring data with a URL syntax. The following examples show how to use cURL with Gecko OS.

In some of the examples below, curl commands and response are shown in separate lines for readability purposes. Curl commands should be issued on a single command line.

Tip! If you are having trouble using curl, try using the -v verbose flag.

Gecko OS Version

Curl Command
curl http://mydevice.local/command/version
Gecko OS Response
{"id":18,"code":0,"flags":0, "response":"SILABS-WGM160P-4.0.0, Gecko_OS-STANDARD-4.0.12-1198, WGM160P"}

Curl used with a URL only issues an HTTP GET request.

Verbose file list

This example reads a verbose list of files on the device and uses the more sophisticated POST request API . Note that the double-quotes may need to be escaped with backslash characters (as shown below) on some operating systems, such as Windows.

Curl used with the -d ( --data ) option issues an HTTP POST request. Note that the Content-Type header must be supplied to specify that the data posted is in json format.

Curl Command
curl -H "Content-Type: application/json" -d "{\"flags\":0,\"command\":\"ls -v\"}" http://mydevice.local/command
Gecko OS Response
{"id":3,"code":0,"flags":0, "response":"! # Type Flags Hnd Size Version Filename\r\n # 0 e-FB 0001 82 41741 2.1.0.0 command_help.csv\r\n # 1 e-FD 0001 53 135 2.1.0.0 default_setup.script\r\n # 2 e-FE 0001 81 1897 2.1.0.0 favicon.ico.gz\r\n # 3 e-03 0001 52 1236 2.1.0.0 geotrust_ca.pem\r\n # 4 i-00 801B 0 212736 2.1.0.0 sys/kernel.bin\r\n # 5 i-81 801B 52 215204 2.1.0.0 sys/services.bin\ r\n # 6 e-FE 0001 55 38004 2.1.0.0 gecko_os_webgui.css.gz\r\n # 7 e-FE 0001 54 1827 2.1.0.0 gecko_os_webgui.html\r\n # 8 e-FE 0001 65 61492 2.1.0.0 gecko_os_webgui.js.gz\r\n # 9 e-01 0009 0 210412 5.26.230.12 wifi_fw.bin\r\n"}

The value of the response key is the return data from the command. In this case it is the list of files. This is formatted to be read on a terminal in a monospace font, with new lines where each \r\n character sequence appears.

Open and Read a file

Open the default_setup.script file located on the Gecko OS file system:

Curl Command
curl -H "Content-type: application/json" -d "{\"flags\":0,\"command\":\"file_open default_setup.script\"}" http://mydevice.local/command
Gecko OS Response
{"id":14, "code":0,"flags":0, "response":"0\r\n"}

The value of the response key is the return data from the command. In this case it is the stream number for the open file stream, 0 , which is used as an argument to the stream_read command issued below.

Read the file using the stream_read command:

Curl Command
curl -H "Content-type: application/json" -d "{\"flags\":0,\"command\":\"stream_read 0 300\"}" mydevice.local/command
Gecko OS Response
{"id":15,"code":0,"flags":0, "response":"network_up,-s ,Configuration network credentials\r\nset wlan.auto_join.enabled,true,Enable network auto-join\r\nsave,-,Saving settings\r\n\r\n"}

Supporting Gecko OS Versions

Change Log

Modified Changes
2019-01-01 Created