LabVIEW Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
DerekTrepanier

Add support for OPTIONS request to LabVIEW webservice

Status: New

To support modern web thin clients, the LabVIEW webservice needs to support the OPTIONS request. When a web browser makes a cross origin request there are a few rules that the response must comply with before the browser will provide with the result to the web application. The OPTIONS request is needed to respond to these requests.

 

A cross origin request is any request where an application running on one domain needs to make a request to another domain. For example, if there was a javascript application running on the domain www.labview.com and it needed to make a AJAX request to api.labview.com, the browser considers this a cross origin request. This is a common setup when the thin client is being hosted by one server, and the webservice is being hosted by another server. https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

 

Depending on the request, the browser may issue a "preflight" request. "Standard" requests are GET and POST requests without custom header fields only require the response has an additional headers. Other requests (PUT, DELETE, GET w/ headers, POST w/ headers) require a preflight request. A preflight request is an OPTIONS request. If the OPTIONS request doesn't get an appropriate response, the browser will not even make the actual request. https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request

 

For example, I'm starting an application and here's how I'd like it setup.

 

1. Windows PC. This is the central server for the application. There will be a normal LabVIEW application running on this computer. There will also be a web service run on this computer. The web service will mainly host a web application written using HTML/CSS/Javascript (although it could be written using HTML vis from NXG).

 

2. A bunch of CRIOs. These CRIOs will also host their own web service to get system information, measurements, and diagnostics. As new CRIOs are commissioned, they'll report to the Windows PC.

 

Now a user uses their web browser to access to the windows PC. The Javascript Application (which is hosted on the Windows web service) provides the user with a list of all commissioned CRIOs. They then click on one and the javascript application starts to make requests directly to the CRIO's web service. Since these CRIO's are on a separate IP address, the browser flags this as a CORS request. This means that an OPTIONS request can be sent. Since I can't handle an OPTIONS request in a LabVIEW web service, I can't implement this.

 

This is just one example. Another case would be an Apache hosted web application hosted on an Amazon EC2 needs to make requests to a LabVIEW web service running in a different domain. Basically any time a web application (AKA something running in the browser) needs to communicate with a LabVIEW web service you'll run into this problem.

2 Comments
AgentAstronaut
Active Participant

I'm not super familiar with HTTP in general, but couldn't this be done with the Set HTTP Header VI?

 

For clarification, how is this different then adding a CORS header like below.AddCORSHeaders.png

Michael Bilyk
Former NI Software Engineer (IT)
DerekTrepanier
Member

This covers "Standard" requests. As I stated:


@DerekTrepanier wrote:

"Standard" requests are GET and POST requests without custom header fields only require the response has an additional headers. Other requests (PUT, DELETE, GET w/ headers, POST w/ headers) require a preflight request

 So if your API only supports GET and POST requests, without additional headers, you're fine. If you're designing an API that follow modern best practices (such as https://docs.microsoft.com/en-us/azure/architecture/best-practices/api-design) you need PUT and DELETE methods. If you use modern authentication techniques (which are typically JWT https://jwt.io/introduction/ based) then you'll need additional headers.

This means your CORS enabled webserver needs to respond to preflight OPTIONS requests.