It seems that running javascript from Archi is a bit limited compared to running js from a browser. E.g. in Jarchi the XMLHttpClient library is not available which makes it kind of hard to call REST API's from jarchi script.
Any ideas on how to call rest APIs from jarchi via other ways than popular js libraries based on XMLHttpClient are doing (e.g. Fetch or Axios)? It would be awesome if jarchi would allow calling REST APIs. I have got the feeling that achieving this is well within reach if a tiny missing piece of the puzzle can be found.
The JS engine uses Nashorn which doesn't support as much as full JS. For example - https://blog.codecentric.de/en/2014/06/project-nashorn-javascript-jvm-polyglott/
"Nashorn is not the same as Node.js nor is it the same as the JavaScript environment in the browser. Nashorn is only a JavaScript engine, i.e. an implementation of the ECMAScript 5.1 language specification plus memory management. This means that global JavaScript functions such as setTimeout, setInterval or XMLHttpRequest do not exist in Nashorn. Neither does it have an event loop or a task queue. This means that many JavaScript libraries cannot be used with Nashorn because such libraries commonly expect a browser-like environment. "
Hi,
In fact Nashorn implement JavaScript itself, but things like XMLHttpRequest are not part of JS itself but offered as "Add ons" by browser or Node.JS.
But fortunately, some people have developped polyfills (https://www.npmjs.com/package/nashorn-polyfill) for Nashorn and some ad-hoc libs (https://gist.github.com/billybong/a462152889b6616deb02) using underlying JVM.
Regards,
JB
Thanks JB!
I got it to work with: https://gist.github.com/billybong/a462152889b6616deb02 (https://gist.github.com/billybong/a462152889b6616deb02)
This script does not seem to work with https but for now this is not a problem for me. Maybe there are some other Nashorn examples available that also work with https.
Regards
Roy
Hi,
Quote from: rfamans on October 09, 2018, 20:25:59 PM
This script does not seem to work with https but for now this is not a problem for me.
I haven't tested it but it should work. Are you targeting a self signed HTTPS server? If yes this might explain the issue.
Quote from: rfamans on October 09, 2018, 20:25:59 PM
Maybe there are some other Nashorn examples available that also work with https.
If you find one please let me know as I might need it in the future.
Regards,
JB
Hi JB,
For now, https is not required. If https is to be used, it will be done with proper certificates.
I will keep you posted.
//Roy
Did anyone find a version of this that worked with https?
I too am wondering about https
Before i moved to JRuby, i used java.net.URL directly from JS:
function getStreamFromURL(url) {
with (new JavaImporter(java.io, java.net.URL)) {
var is = new URL(url).openStream();
try {
var reader = new BufferedReader(
new InputStreamReader(is));
var buf = '', line = null;
while ((line = reader.readLine()) != null) {
buf += line;
}
} finally {
reader.close();
}
return buf;
}
}
....
var data = JSON.parse(getStreamFromURL(Url));