Page 1 of 1

Query an API for XML Job Ticket

Posted: Mon Aug 19, 2013 1:41 am
by Sunnyland
I have created an API and was hoping to use switch to query the API to get the jobticket information. I have setup so that when the job ticket id is sent to the API a XML file is created and then saved into an input folder but I cannot get the script to query the API. My Javascript is not very good and the only examples I could find was to use document.location.href = but at this point I cannot get it to work. Any Ideas?

Query an API for XML Job Ticket

Posted: Mon Aug 19, 2013 3:48 pm
by dkelly
Is your API a web services, eg. SOAP, command-line app or script?

Query an API for XML Job Ticket

Posted: Tue Aug 20, 2013 9:32 am
by freddyp
document.location.href is a typical code snippet from Javascript used in a browser environment. Anything you find on Javascript for browsers is useless in a Switch context. Use the scripting reference in the Switch help instead. You will find that Javascript in a Switch environment can do a lot more than Javascript in a browser environment.



Use the internet only for matters of syntax and methods of basic declaration types (although the documentation in the Switch help on basic Javascript is also pretty extensive and I prefer to use that).



You should write a script that emulates the behavior of "XML pickup": an incoming file triggers the scripts; it calls the API, which creates the XML, and it attaches the XML as a dataset.



As Dwight implicitly wrote: how you call the API from the script will depend on how the API works.



Freddy

Query an API for XML Job Ticket

Posted: Fri Sep 13, 2013 5:28 pm
by alexbevi
A little late to the party here, but all the APIs we work with we tend to use either curl or wget in a javascript script (via Process.execute), then store the results (depending on the desired action).

Re: Query an API for XML Job Ticket

Posted: Wed Oct 02, 2019 4:19 am
by Sunnyland
Hi All,
Thanks for the responses. After a little research I found some code that did the trick for me. Thought I would share in case it helps someone else

var theHTTP = new HTTP( HTTP.NoSSL );
theHTTP.url = "URL"; // This script file is also in the repo. Needs to go on an http server

job.log( 1, "Url: %1", theHTTP.url );

var postBody = new ByteArray( postJSON , "UTF-8" );
theHTTP.setPostData( postBody, "application/json" );
var theResponseFilePath = job.createPathWithName( job.getNameProper(), false );

theHTTP.localFilePath = theResponseFilePath;
theHTTP.post();

while( !theHTTP.waitForFinished( 3 ) ) { }
job.log( 1, "Request finish status: %1", theHTTP.finishedStatus );

job.sendToSingle(job.getPath());

return "Request finish status: " + theHTTP.finishedStatus ;