11-24-2020 11:45 AM
Are there any examples that leverage the Systemlink REST API in native javascript?
I have managed to write some python scripts to perform queries and execute actions via the REST API, but there are some things I'd love to be able to do natively within a web browser. However, I'm having issues performing the same REST operations with native javascript that I have been able to do in python.
As an example, for something simple like performing the "querysystems" POST operation, I can do that via python doing something like this (where "auth_details" includes username & password):
# Make the API call (POST)
r = requests.post(
f"https://{systemlink_server}/nisysmgmt/v1/query-systems",
data=json.dumps(arg),
verify=False,
auth=auth_details,
)
systems = json.loads(r.content)
11-25-2020 02:58 AM - edited 11-25-2020 03:04 AM
It is easiest for a webpage to do use the REST API if the webpage is hosted on the SystemLink server itself as a SystemLink plugin.
You can do so by deploying it to C:\Program Files\National Instruments\Shared\Web Server\ . There's a simple template at https://github.com/ni/systemlink-web-interface-template/tree/master/Web%20Server that shows you what files need to go where. If you have the NXG Web Module, you can even clone the whole repo and build an .nipkg file to install on the server.
Here's a simplified version of a function I call from my SystemLink-hosted web page, which works just fine without disabling CORS on the server (assuming that the logged-in user has file creation permissions in SystemLink):
function postSystemLinkFile(fileContent, filename)
{
let formData = new FormData();
formData.append("file", fileContent);
formData.append("metadata", JSON.stringify({
'Name': filename,
'Property1': 'Value1',
'Property2': 'Value2'
}));
let request = new XMLHttpRequest();
request.open("POST", "/nifile/v1/service-groups/Default/upload-files");
request.send(formData);
}
@TurboPhil wrote:
there are some things I'd love to be able to do natively within a web browser. However, I'm having issues performing the same REST operations with native javascript that I have been able to do in python.
Web browsers and servers need to be extra vigilant to protect users against malicious websites on the Internet -- that's why your webpage faces CORS restrictions but your Python environment doesn't have to worry about it.
12-01-2020 10:44 AM
Ok, I have managed to get a basic page up and hosted directly on our Systemlink server, using the Github repo you pointed to as an example.
Unfortunately, we don't have the NXG Web Module license, so we don't get any of those out-of-the-box packaging features. So far, I just copied over the web application from that Github repo and did manual hacking to add a javascript file with some ajax calls to hit the system manager / package repo APIs. It's functional, but unwieldy. It would be nice if the files were a bit more consolidated, so I could set up a Git repo that could be used to push the changes over to the server. Is that something that is on the radar for Systemlink? Or are there any other ways of updating custom apps/pages on the server that don't require NXG?
12-01-2020 10:32 PM - edited 12-01-2020 10:47 PM
Congrats!
@TurboPhil wrote:
So far, I just copied over the web application from that Github repo and did manual hacking to add a javascript file with some ajax calls to hit the system manager / package repo APIs.... Or are there any other ways of updating custom apps/pages on the server that don't require NXG?
You can invoke the NI Package Manager directly to create a package without the NXG Web Module. Put your deployment files in the appropriate folder structure, add some metadata files, and create a File Package: https://www.ni.com/documentation/en/ni-package-manager/20.5/manual/assemble-file-package/
You'll still need to transfer the *.nipkg file to your server to install it.
Here's what I did:
It might also be possible to do this via the NI Package Builder which has a decent GUI, but I haven't tried it: https://www.ni.com/documentation/en/ni-package-builder/latest/manual/manual-overview/