LabVIEW Web Services - The RESTful CRUD

This post is part of the LabVIEW Web Services Series, which covers topics from install LabVIEW to creating an AJAX web app powered by LabVIEW.

RESTful

A powerful design model for web apps is the RESTful API. You can read the full details on Wikipedia Representational state transfer. For the TL;DR crowd, a RESTful API loosely defines mapping the HTTP methods (GET, POST, PUT, DELETE) to data creation and retrieval. The API is stateless and all data is stored server side. The stateless architecture allows for clients to disconnect and reconnect at will.

CRUD

HTTP defines several methods, but in the RESTful architecture only four methods are used. These four methods correspond to the four CRUD functions and are POST, GET, PUT, and DELETE.

CRUD HTTP method
Create POST
Read GET
Update PUT
Delete DELETE

REST is a style and not a standard and there are many variations on how it is implemented. For most web applications the four HTTP methods are mapped to the same URL. For example a GET, POST, PUT or DELETE can be sent to http://127.0.0.1/front-panel-controls/slide and the corresponding CRUD operation will be performed on the slide end point.

Some low power devices might not be capable of the four HTTP methods and so the REST API is modified to work with only the GET method and a Query String. In this way the URL is modified to be http://127.0.0.1/front-panel-controls/slide/read or http://127.0.0.1/front-panel-controls/slide/update?slide=2. This method is also great for testing with a web browser. A web browser makes an HTTP GET request on the address in the address bar and displays the server response on the page.

LabVIEW Web Service

Create a blank project, add a Web Service and rename it to "front-panel-controls" then create a new VI and save it as "Main". The Main VI has two functions: first it reads a global and writes it to a vertical slider, second it writes the global when the OK button is pressed.

For a quick refresher on Web Resources check out my previous post LabVIEW Web Services - Create a Web Resource. Right click on Web Resource and select New Web Resource. Name the New Web Resource "slide". Right click "slide" and select New VI. Save the new VI as "read" and make sure the method is set to GET. Add a numeric indicator and name it slide, then link it to one of the VI's terminals.

Add another VI to the "slide" Web Resource and save this VI as "update" and make sure the method is set to GET. This VI will read in the Query String and write the value for "slide" to the LabVIEW Global variable slide. So that the web browser gets a valid response add a numeric indicator and link it to one of the VI's terminals.

Run the Main.vi and Start the Web Service. Right click the read.vi and select Show Method URL. Copy the URL and past it into a browser, the current value for slide should be returned in the XML.

Then copy the Method URL for update.vi and paste it into a web browser's address bar. Before submitting the address add the Query String ?slide=4 to the end of the URL. Once this address is submitted to the web browser the value of slide should be updated in Main.vi.

This is a very simple REST API written in LabVIEW Web Services. The other two methods, create and delete, can be added in similar fashion to update. Any program that can make an HTTP GET request can now interact with your LabVIEW programs.

Download the LabVIEW Code

Read More