...
- Basically a web service call is implemented by executing one of the above methods from the JITL Java class, e.g.:
var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://localhost:444454445/jobscheduler/agent/api/" );
var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand( "get", "http://localhost:444454445/jobscheduler/agent/api/" );
- The web service returns a serialized JSON Object:
- The result object returned by the web service can be converted to a JavaScript JSON object by use of the
eval()
function:eval ( "var jsonObject = " + response + ";" );
- Properties of the JSON object can be accessed within the object hierarchy provided by the web service result object like this:
see example for
jsonObject.system.hostname
- The result can be parsed using the SOSXMLXPath Java class.
- see example below
from the JobScheduler Agent REST web service
- see example below
below:
Code Block language js title Example for REST web service client processing a JSON response linenumbers true collapse true function spooler_process() { var
- The result object returned by the web service can be converted to a JavaScript JSON object by use of the
...
response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://
...
localhost:4445/jobscheduler/agent/api/" );
...
if (response) { eval ( "var jsonObject = " + response + ";" ); spooler_log.info(
...
jsonObject.totalTaskCount + " tasks executed on Agent [" +
...
rest_service + "]: " + jsonObject.system.hostname ); } else {
...
Examples
Simple REST Client Example
- JavaScript example for use with a job: this job requests the status information from the JobScheduler Universal Agent and returns the total number of tasks that have been executed during the Agents' lifetime. This job reads the web service URL from a job parameter.
- Download: rest_client.job.xml
...
language | xml |
---|---|
title | Job rest_client |
collapse | true |
...
spooler_log.error( "no response from REST web service at: " + rest_service );
...
}
...
...
return false; }
- The web service returns an XML document:
- The result can be parsed using the SOSXMLXPath Java class.
see example below
Code Block language js title Example for REST web service client processing an XML response linenumbers true collapse true function spooler_process() {
- The result can be parsed using the SOSXMLXPath Java class.
...
...
var
...
response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/18/" ); spooler_log.info( response ); var xmlDOM = new Packages.sos.xml.SOSXMLXPath( new java.lang.StringBuffer( response ) ); spooler_log.info( "Firstname is " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/FIRSTNAME" )); return false; }
Examples
Simple standalone Job with REST Client
- JavaScript example for use with a job: this job requests the status information from the JobScheduler Universal Agent and returns the total number of tasks that have been executed during the Agents' lifetime. This job reads the web service URL from a job parameter.
- Download: rest_client.job.xml
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<job stop_on_error="no" > <params >spooler.create_variable_set(); parameters.merge( spooler_task.params ); var agent_service = parameters.value( "agent_service" ); var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( agent_service ); // alternative REST methods // var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://localhost:44445/jobscheduler/agent/api/" ); // var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand("get", "http://localhost:4445/jobscheduler/agent/api/" ); // var response = com.sos.jitl.restclient.JobSchedulerRestClient.getRestService( "localhost", 4445, "/jobscheduler/agent/api/", "http" ); if (response) { eval<param ( "var jsonObject = " + response + ";" ); name="agent_service" value="localhost:4445/jobscheduler/agent/api/"/> </params> <script language="java:javascript"> <![CDATA[ function spooler_log.infoprocess( jsonObject.totalTaskCount + " tasks on Agent [" + agent_service + "]: " + jsonObject.system.hostname) { var parameters = spooler.create_variable_set(); parameters.merge( spooler_task.params ); } else {var agent_service = parameters.value( "agent_service" ); var response spooler_log.error( "no response from Agent web service at: " + = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( agent_service ); // alternative REST }methods // var response return (response != ""); } ]]> </script> <run_time /> </job> |
Full Example with GET|POST|PUT|DELETE and XML Responses
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
function spooler_process() { var s = = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://localhost:4445/jobscheduler/agent/api/" ); // var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand("get", "http://localhost:4445/jobscheduler/agent/api/" ); // var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServicegetRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/18/localhost", 4445, "/jobscheduler/agent/api/", "http" ); spooler_log.info( s); var xmlDOM = new Packages.sos.xml.SOSXMLXPath( new java.lang.StringBuffer( s ) ); if (response) { eval ( "var jsonObject = " + response + ";" ); spooler_log.info( jsonObject.totalTaskCount + "Firstname is tasks on Agent [" + agent_service + "]: " + jsonObject.system.hostname ); } else { spooler_log.error( "no response from Agent web service at: " + agent_service ); } return false; } ]]> </script> <run_time /> </job> |
Full standalone Job with GET|POST|PUT|DELETE and XML Responses
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
<job stop_on_error="no" > <params/ > <script language="java:javascript"> <![CDATA[ function spooler_process() { //Getting the name of a personxmlDOM.selectSingleNodeValue( "//CUSTOMER/FIRSTNAME" )); //Creating a person with post com.sos.jitl.restclient.JobSchedulerRestClient.headers.put("Content-Type", "application/xml"); var post = "post(<resource><ID>4711</ID><LASTNAME>world</LASTNAME><FIRSTNAME>Hello</FIRSTNAME></resource>)"; var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand(post, "http://www.thomas-bayer.com/sqlrest/CUSTOMER/" ); //Reading the new entry var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" ); var xmlDOM = new Packages.sos.xml.SOSXMLXPath( new java.lang.StringBuffer( s ) ); spooler_log.info( "Name is " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/FIRSTNAME" ) + " " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/LASTNAME" )); //Changing the name com.sos.jitl.restclient.JobSchedulerRestClient.headers.put("Content-Type", "application/xml"); var post = "post(<resource><FIRSTNAME>Uwe</FIRSTNAME></resource>)"; var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommandexecuteRestService(post, "http://www.thomas-bayer.com/sqlrest/CUSTOMER/471118/" ); //Reading the name var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/18/" spooler_log.info( s ); var xmlDOM = new Packages.sos.xml.SOSXMLXPath( new java.lang.StringBuffer( s ) ); spooler_log.info( "Firstname is " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/FIRSTNAME" )); //DeletingCreating thea newperson entrywith post com.sos.jitl.restclient.JobSchedulerRestClient.headers.put("Content-Type", "application/xml"); var post = "post(<resource><ID>4711</ID><LASTNAME>world</LASTNAME><FIRSTNAME>Hello</FIRSTNAME></resource>)"; var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand("delete"post, "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" ); //Reading the deletednew entry var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" ); spooler_log.info( s); //Creating a person with put comvar xmlDOM = new Packages.sos.jitl.restclient.JobSchedulerRestClient.headers.put("Content-Type", "application/xml"xml.SOSXMLXPath( new java.lang.StringBuffer( s ) ); var put = "put(<resource><LASTNAME>world</LASTNAME><FIRSTNAME>Hello</FIRSTNAME></resource>)"; var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand(put, "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711" spooler_log.info( "Name is " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/FIRSTNAME" ) + " " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/LASTNAME" )); //ReadingChanging the new entryname var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceheaders.put( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" "Content-Type", "application/xml"); var xmlDOMpost = new Packages.sos.xml.SOSXMLXPath( new java.lang.StringBuffer( s ) ); spooler_log.info( "Name is " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/FIRSTNAME" ) + " " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/LASTNAME" )); //Deleting the new entry com.sos.jitl.restclient.JobSchedulerRestClient.headers.put("Content-Type", "application/xml"); var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand("delete", "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" ); //Reading the deleted entry var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" ); spooler_log.info( s); return false; } |
...
"post(<resource><FIRSTNAME>Uwe</FIRSTNAME></resource>)";
var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand(post, "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" );
//Reading the name
var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/18/" );
var xmlDOM = new Packages.sos.xml.SOSXMLXPath( new java.lang.StringBuffer( s ) );
spooler_log.info( "Firstname is " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/FIRSTNAME" ));
//Deleting the new entry
com.sos.jitl.restclient.JobSchedulerRestClient.headers.put("Content-Type", "application/xml");
var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand("delete", "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" );
//Reading the deleted entry
var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" );
spooler_log.info( s);
//Creating a person with put
com.sos.jitl.restclient.JobSchedulerRestClient.headers.put("Content-Type", "application/xml");
var put = "put(<resource><LASTNAME>world</LASTNAME><FIRSTNAME>Hello</FIRSTNAME></resource>)";
var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand(put, "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711" );
//Reading the new entry
var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" );
var xmlDOM = new Packages.sos.xml.SOSXMLXPath( new java.lang.StringBuffer( s ) );
spooler_log.info( "Name is " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/FIRSTNAME" ) + " " + xmlDOM.selectSingleNodeValue( "//CUSTOMER/LASTNAME" ));
//Deleting the new entry
com.sos.jitl.restclient.JobSchedulerRestClient.headers.put("Content-Type", "application/xml");
var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand("delete", "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" );
//Reading the deleted entry
var s = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestService( "http://www.thomas-bayer.com/sqlrest/CUSTOMER/4711/" );
spooler_log.info( s);
return false;
}
]]>
</script>
<run_time />
</job> |
Full Job Chain with GET|POST|PUT|DELETE and JSON Responses
Download the example: rest_client.zip
Job: rest_client
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
<job stop_on_error="no" order="yes">
<params />
<script language="java:javascript">
<![CDATA[
function spooler_process() {
// for sample REST servivce see https://github.com/typicode/jsonplaceholder#how-to
var parameters = spooler.create_variable_set();
parameters.merge( spooler_task.params );
parameters.merge( spooler_task.order.params );
var rest_service = parameters.value( "rest_service" );
var rest_operation = parameters.value( "rest_operation" );
var rest_content_type = parameters.value( "rest_content_type" );
switch( rest_operation.toLowerCase() ) {
case "get":
var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand( rest_operation, rest_service );
if (response) {
eval ( "var jsonObject = " + response + ";" );
spooler_log.info( "REST service GET response [" + rest_service + "]: " + jsonObject.title );
} else {
spooler_log.error( "no response from REST service at: " + rest_service );
}
break;
case "post":
// use post( ... ) to forward content to REST service
switch ( rest_content_type.toLowerCase() ) {
case "xml":
com.sos.jitl.restclient.JobSchedulerRestClient.headers.put("Content-Type", "application/xml");
var post = "post( <resource><ID>4711</ID><LASTNAME>world</LASTNAME><FIRSTNAME>Hello</FIRSTNAME></resource> )";
var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand( post, rest_service )
if (response) {
spooler_log.info( "REST service POST response [" + rest_service + "]: " + response );
} else {
spooler_log.error( "no response from REST service at: " + rest_service );
}
break;
case "json":
com.sos.jitl.restclient.JobSchedulerRestClient.headers.put( "Content-Type", "application/json" );
var post = "post( { title: 'foo', body: 'bar', userId: 1 } )";
var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand( post, rest_service );
if (response) {
eval ( "var jsonObject = " + response + ";" );
spooler_log.info( "REST service POST response [" + rest_service + "]: " + jsonObject.title );
} else {
spooler_log.error( "no response from REST service at: " + rest_service );
}
break;
default:
spooler_log.error( "invalid content type [rest_content_type]: " + rest_content_type );
var response = "";
}
break;
case "put":
// use put( ... ) to forward content to REST service
com.sos.jitl.restclient.JobSchedulerRestClient.headers.put( "Content-Type", "application/json" );
var put = "put({ title: 'foo', body: 'bar', userId: 1 })";
var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand( put, rest_service );
if (response) {
eval ( "var jsonObject = " + response + ";" );
spooler_log.info( "REST service PUT response [" + rest_service + "]: " + jsonObject.title );
} else {
spooler_log.error( "no response from REST service at: " + rest_service );
}
break;
case "delete":
var response = com.sos.jitl.restclient.JobSchedulerRestClient.executeRestServiceCommand( "delete", rest_service );
if (response) {
spooler_log.info( "REST service DELETE response [" + rest_service + "]: " );
} else {
spooler_log.error( "no response from REST web service at: " + rest_service );
}
break;
default:
spooler_log.error( "invalid REST operation (get|put|post|delete): " + rest_operation );
var response = "";
}
return (response != "");
}
]]>
</script>
<run_time />
</job> |
Job Chain: rest_client
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
<job_chain title="Call REST service with HTTP GET|POST|PUT|DELETE operation" name="rest_client">
<job_chain_node state="rest_client" job="rest_client" next_state="success" error_state="error"/>
<job_chain_node state="success"/>
<job_chain_node state="error"/>
</job_chain> |
Orders: rest_client
Find additional orders for REST operations with the download of this example.
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
<order job_chain="rest_client" id="rest_client_post_json">
<params >
<param name="rest_service" value="http://jsonplaceholder.typicode.com/posts"/>
<param name="rest_operation" value="post"/>
<param name="rest_content_type" value="json"/>
</params>
<run_time />
</order> |
References
See also
...