SystemLink Forum

cancel
Showing results for 
Search instead for 
Did you mean: 

Systemlink API requests via javascript

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)
 
 
But when I try doing similar things in javascript, I'm hitting issues with CORS and authentication cookies, etc. Has anyone else attempted this and resolved these issues? Are there any lessons we could learn from?
 
I know, for example, that there is a setting on the Systemlink Server that can enable CORS globally, or for specific servers. For initial testing, I went ahead and enabled it globally, and then leveraged the http-server node package with the "--cors" argument. But now I'm running into issues where requests appear to be returning 302 redirects and expecting session cookies and....I'm out of my depth.
 
Are there any good resources we can leverage to get past these hurdles (and document for other users)?
0 Kudos
Message 1 of 4
(1,766 Views)

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.

Certified LabVIEW Developer
Message 2 of 4
(1,730 Views)

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?

0 Kudos
Message 3 of 4
(1,683 Views)

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/

Certified LabVIEW Developer
0 Kudos
Message 4 of 4
(1,668 Views)