Tutorial: Creating and Publishing a LabVIEW Web Service to the NI Web Server (Windows)

Parent Topic: Communicating with a LabVIEW Application from Web Clients

Complete the following steps to create, publish, and exchange data between a LabVIEW Web service and a web client, in this case, a web browser.

Note  LabVIEW Web services are available only in the LabVIEW Full Development System and the LabVIEW Professional Development System.

This tutorial contains the following sections:

  1. Creating a Web Service
  2. Testing and Debugging the Web Service
  3. Configuring How Clients Access the Web Service
  4. Publishing the Web Service to the NI Web Server
  5. Accessing the Web Service with a Client

Creating a Web Service

Develop a Web service in a LabVIEW project to group together and configure the VIs and other files that make up the Web service. Complete the following steps to create a Web service project and two HTTP method VIs.

  1. Create a LabVIEW project to organize the Web service files. Save the project as Tutorial.lvproj.
  2. Right-click My Computer and select New»Web Service. LabVIEW adds a Web service project item and folders under the target.
    Note Note  In this tutorial, you create a Web service and publish it on the host computer. You also can publish a Web service to a Real-Time (RT) target by creating a Web service project under the RT target in Project Explorer. If you want to publish your Web services to RT targets, you can use only the Application Web Server to host your Web services. Refer to Tutorial: Creating and Publishing a LabVIEW Web Service to the Application Web Server for detailed instructions.
  3. Right-click the Web service project item , select Rename, and name the Web service TutorialService. The project appears as follows:

  4. Right-click Web Resources and select New VI to create a new HTTP method VI, which is a VI that receives HTTP requests from clients and returns data to clients. LabVIEW opens a new VI from a template.
    Note Note  By default, an HTTP method VI returns data to clients via the connector pane. In this tutorial, the HTTP method VIs return the default JSON-formatted text responses. When you create an HTTP method VI outside this tutorial, you also can stream data to a client in a custom format, such as a customized HTML page or image data. Configure the output format type on the HTTP Method VI Settings page of the Web Service Properties dialog box.
  5. In the new VI, build the block diagram, front panel, and connector pane as shown in the following figures.

    Block Diagram Front Panel Connector Pane

    When you publish this VI as part of a Web service, a web client can send an HTTP request to invoke it. When invoked, this VI performs the following actions:
  6. Notice that each control and indicator appears on the connector pane. With the default Terminal output type, Web services can receive data from clients or send data to clients only through inputs and outputs assigned to the connector pane of an HTTP method VI.
    Tip Tip  Assign recognizable names to each object. These names become part of the URL that web clients use to exchange data with the VI.
  7. Save the VI as Add.vi.
  8. Select File»Save As and select Copy»Open additional copy in the dialog box that prompts. Ensure the Add copy to Tutorial.lvproj checkbox contains a checkmark, and then click Continue.
  9. Save the new copy as Subtract.vi.
  10. Open the block diagram of Subtract.vi and replace the Add function with the Subtract function. Rewire the controls and indicators.
  11. Return to the project, right-click Web Resources, and select New Web Resource. Rename the new project item AdditionalOperations.
  12. Drag Subtract.vi under the AdditionalOperations project item. Save the project, which should appear as in the following figure.

Notice the following features in the project tree:

Related Information

Components of a Web Service

Assigning Terminals to Controls and Indicators

Developing HTTP Method VIs: Choosing the Format of Output Data

Configuring HTTP Headers, Streaming, and Buffering

Managing a Project in LabVIEW

Testing and Debugging the Web Service

Before you publish the Web service to a target, you might want to test that the HTTP method VIs communicate with clients as expected. You can run the Web service on the NI Web Server directly from the LabVIEW project to allow a client to send requests to HTTP method VIs.

  1. Right-click the Web service project item and select Start.
  2. If you are using the NI Web Server for the first time on your computer, complete the following steps to enable and configure the NI Web Server:
    1. Click Configure NI Web Server in the Start Web Service dialog box to launch NI Web Server Configuration.
    2. For this tutorial, choose the Simple local access configuration preset and click Next.
    3. On the Authentication tab, select Log in using an admin user for server administration and create your password. Click Next.
    4. Click Finish to apply this configuration.
  3. Switch back to the LabVIEW project and repeat step 1. The Web service is now running on the NI Web Server.
  4. Open Add.vi. Notice that the Run button indicates the VI is reserved for execution because LabVIEW loads the Web service and makes it available on the NI Web Server for communication with clients.
  5. Close Add.vi.
  6. Complete the following steps to get the URL that clients use to invoke Add.vi:
    1. Right-click Add.vi and select Show Method URL to display the HTTP Method URL dialog box.
    2. In the Available Servers pull-down menu, select the item that contains NI Web Server, and then click Copy URL.
    3. Close the dialog box.
      Note Note  The Web service does not respond to any requests from the client if the HTTP Method URL dialog box is open.
  7. Paste the URL from step 6 into a standard web browser and replace the {value} variables with numeric values, such as: http://127.0.0.1:80/TutorialService/Add?b=2&a=3.
    Note Note  NI Web Server uses 80/443 as the default port in accordance with HTTP/HTTPS protocol. However, the port number might change if another application occupies the port. You can customize the port number in NI Web Server Configuration. Refer to Configuring NI Web Server for more information on server settings like authentication, port, and remote connections.
  8. Browse to the URL. Add.vi receives the HTTP request from the browser, computes the sum of the values for a and b, and returns a response with the name and output value of each indicator assigned to the connector pane of Add.vi. In this tutorial, the response is a JSON string, which is the default Terminal output format. Recall that Add.vi has two indicators assigned to its connector pane: c and value, whose values the HTTP method VI returns in the response.

    {"c":5,"value":"b=2&a=3"}

    Note Note  If you notice unexpected behavior, you must stop the debugging session before you can edit the Web service files because you cannot edit the files while LabVIEW reserves them for execution. Right-click the Web service project item and select�Stop.
  9. Repeat steps 6 and 7 for Subtract.vi, or update the URL from step 7 to reflect the unique URL mapping for Subtract.vi: http://127.0.0.1:80/TutorialService/AdditionalOperations/Subtract?b=2&a=3.
  10. Repeat step 8 for Subtract.vi.
  11. Right-click the Web service project item and select Stop.

Related Information

Testing and Debugging a Web Service

Setting up the NI Web Server for Web Services

Configuring How Clients Access the Web Service

Web clients use URLs to exchange data with HTTP method VIs. Later in this tutorial, you will enter a specific URL in a web browser to send data directly to the controls on the connector pane of the HTTP method VIs.

Complete the following steps to define the URLs clients can use to access the HTTP method VIs:

  1. Right-click the Web service project item and select Properties to display the Web Service Properties dialog box.
  2. On the HTTP Method VI Settings page, notice that HTTP method VIs from the Web Resources folder in the LabVIEW project appear in the table of Web service VIs. Further, notice how placing Subtract.vi under the AdditionalOperations project item affects the URL mapping compared to Add.vi.
  3. Select Subtract.vi in the table and notice the following features on the URL Mapping tab at the bottom of the page:
    Note Note  In a more complex Web service, you might use the remaining tabs on this page to configure the format that the VI returns output data to the web client, as well as security settings like privileges.
  4. Click OK to close the Web Service Properties dialog box.
  5. Save Tutorial.lvproj.

Related Information

Understanding URL Mappings and Query Strings in Web Services

Publishing the Web Service to the NI Web Server

You can publish the Web service you create to the NI Web Server. The NI Web Server is a production-grade web server that can host user-authored services, such as LabVIEW Web services, and SystemLink� services created by NI. The NI Web Server protects web applications against common web security threats, provides high scalability to many enterprise-grade data services, and allows device management. In this tutorial, you publish the web service by including it in an NI package and installing the package.

Note Note  You can also publish your Web service to the Application Web Server. Refer to Tutorial: Creating and Publishing a LabVIEW Web Service to the Application Web Server for more details.

Complete the following steps to publish the Web service to the NI Web Server running on the local computer through a package:

  1. Right-click the Build Specifications in the�Project Explorer�window and select�New»Package to launch the�Package Properties dialog box.
  2. On the Information page, specify the location to build the package in the Package output directory text box.
  3. On the�Web Services�page, place a checkmark in the TutorialService checkbox to include the Web service in the package.
  4. Click Build and the Build Status dialog box appears.
  5. When LabVIEW completes building the package, click Explore to open the destination directory where LabVIEW saves the package file (.nipkg).
  6. Double-click the package to install it through the NI Package Manager. When the installation is complete, the Web service is published to the NI Web Server on the host computer.
  7. Close the LabVIEW project. Published Web services run on the LabVIEW Runtime and do not depend on the LabVIEW Development System.

Related Information

Publishing Web Services

Accessing the Web Service with a Client

Complete the following steps to send data to the published Web service from a web browser via a URL:

  1. Paste the URL from step 7 in the Testing and Debugging the Web Service section into a standard web browser and replace the {value} variables with numeric values, such as: http://127.0.0.1:80/TutorialService/Add?b=2&a=3.
    Note Note  If the client is located on a different system than where you publish the Web service, you must replace 127.0.0.1 with the actual IP address of the hosting system or the hostname you customize in NI Web Server Configuration.
  2. The browser returns a JSON response with the output values from each indicator assigned to the connector pane.

Related Information

Sending Data to a Deployed Application Using URLs

Where to Go Next

Example Code

Refer to the following LabVIEW projects for examples of using Web services: