Query an API for XML Job Ticket

Post Reply
Sunnyland
Member
Posts: 56
Joined: Mon Aug 19, 2013 1:34 am

Query an API for XML Job Ticket

Post 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?
dkelly
TOP CONTRIBUTOR
Posts: 658
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Query an API for XML Job Ticket

Post by dkelly »

Is your API a web services, eg. SOAP, command-line app or script?
freddyp
Advanced member
Posts: 1008
Joined: Thu Feb 09, 2012 3:53 pm

Query an API for XML Job Ticket

Post 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
alexbevi
Newbie
Posts: 6
Joined: Thu Feb 14, 2013 6:43 pm

Query an API for XML Job Ticket

Post 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).
Sunnyland
Member
Posts: 56
Joined: Mon Aug 19, 2013 1:34 am

Re: Query an API for XML Job Ticket

Post 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 ;
Post Reply