A Web service uses the same infrastructure as the web, i.e., web servers and clients, HTTP, URLs, and so on, but is intended for machine-to-machine exchange of specific information.
LabVIEW provides
HTTPHyperText Transport Protocol, the basis of the “world wide web”
VIs to implement web clients that can exchange information with web servers that host web services. Secure transfer protocol (HTTPS) implemented by SSL (Secure Socket Layer) is also supported by these VIs.
Example:
Enter the URL http://ipinfo.io in your browser, and the server returns a conventional web page containing graphics and text – a page intended for humans.
Now enter http://ipinfo.io/ip and the server returns nothing more than a character string that contains the IP address of your computer in dot-decimal notation.
This latter URL represents the “endpoint” of a basic Web service – you make a “request” for information by connecting your web client to the Web service endpoint (URL) and the service returns a “response” as a simple text-based web page that contains the requested information.
The simple format requires minimal computational effort for a program to extract the needed information, and is therefore ideal for mobile devices in which computational efficiency translates directly to extended battery life.
Make a different request such as http://ipinfo.io/city and the Web service returns the city name of your Internet Service Provider (ISP).
Web services return structured information most often encoded as ASCII strings in XML (Extensible Markup Language) and JSON (JavaScript Object Notation; pronounced “JAY-suhn”). For example:
The Web service http://ipinfo.io/json returns the IP address and city name as before, but now as part of a complete set of facts about your computer’s Internet connection.
Include the dot-decimal IP address of a specific web server as in http://ipinfo.io/52.9.2.194/json (the dot-decimal address of the ipinfo.io server) to request the same sort of information about that server.
Programming languages including LabVIEW provide tools to easily extract needed information from the JSON string.
Web services are also known as APIs (Application Program Interfaces). For example, the Google Maps Geolocation API endpoint has the form https://maps.googleapis.com/maps/api/geocode/json?address=National+Instruments,+Austin,+TX. The returned JSON string contains the geolocation (latitude, longitude in decimal degrees format) and a wealth of other information about the requested address. The question mark signifies the “GET” method to send a query from the client to the server – the information is included as part of the URL and takes the form of name/value pairs connected by an equals sign; multiple name/value pairs can be included by connecting them by ampersands “&“.
LabVIEW implementation of a Web service client
Study this video to learn about important LabVIEW VIs to call Web services; you may obtain the LabVIEW project files here:
These VIs are described below; locate these elements with “Quick Drop” by pressing Ctrl + Space:
The “GET” VI located in the “Data Communication >> Protocols >> HTTP Client” subpalette acts as a web client and is the foundation of VIs that call web services:
Specify the endpoint URL as a string input, and the VI returns the webpage header and body as separate outputs. The VI waits for the web server to respond for up to 10 seconds (10,000 ms) by default before exiting with an error; shorten or lengthen this time as needed. Study the following video to learn how to use the HTTP “GET” VI:
The name “GET” signifies that this VI implements the “GET” HTTP method, i.e., the query information is included with the URL string. Some web services may require the “POST” HTTP method (see the “POST” VI) in which the query information is transmitted directly.
The LabVIEW HTTP VIs all support SSL (Secure Socket Layer). URLs that begin with “https” indicate that a secure connection should be established to the web server. The entire URL, including the “GET” query string, is secured. This is especially important when the web service requires that you include a username/password, API key, or other sensitive information as part of the URL string.
IMPORTANT: The Academic RIO Device system date and time must be correct to establish an SSL connection. The Device does not include an onboard real-time clock (RTC), therefore its system date resets to the same date on each power cycle. While it is possible to occasionally manually update the power-on system date (see below), consider the inexpensive Digilent I2C battery-backed RTC module to preserve the true calendar date across power cycles.
You can adjust the power-on system date with NI MAX or a browser.
The “Format Into String” and “Concatenate Strings” functions provide convenient tools to assemble the web service request URL.
The “String to IP” and “IP to String” functions may be necessary at times to convert an IP address between dot-decimal format and the equivalent human-readable name.
The “Unflatten From JSON” function accepts the JSON string returned from the web service and parses it into a LabVIEW cluster:
You must create a custom cluster to match the specific form of the JSON response string and wire this to the “type and defaults” input terminal. You need only create LabVIEW constants for the name/value pairs of interest. Open the block diagram of “ipinfo.io IP Lookup API - learn my public IP address” VI and double-click to open the cluster constant connected to “Unflatten From JSON.” Note the labeled string constant that corresponds to each name/value pair in the JSON string. The JSON unflattener returns a cluster of the same type but populated with values read from the string. Double-click on the cluster constant frame to close it again.
Study the following video to learn how to work with the “Unflatten From JSON” function for a JSON string that contains a mixture of string, numeric, array, and Boolean data types, and also how to deal with Web services that return a variable number of elements:
JSON strings can contain nested elements and arrays (see http://json.org/ for complete details). Open the block diagram of “Google Maps API - geocoding” to see a more complex example. The cluster constant contains nested clusters to match the nesting of the JSON string returned by the Google Maps API. However, this is not sufficient, because the JSON string is an array of such nested structures. The string array wired to the “path” input terminal on top of “Unflatten From JSON” directs the VI to extract from the first element (element 0) of the array called “result”. See the LabVIEW Help page for “Unflatten From JSON” for additional details.
The “Unbundle By Name” node picks out the desired elements for individual indicators. You could also create a cluster indicator directly on the output of “Unflatten From JSON”.