Friday, October 11, 2013

How to Perform REST Call from BPM Server Script

IBM BPM has provided a java connector in the system toollkit which will help performing this rest operation. But that is not very robust and doesn't allow us to perform all the kinds of REST operations. In this post I would like to illustrate on how to invoke a REST service from a server script.
Below is the script which can be used to perform a Rest Call from BPM Server sccript.

var client = new Packages.org.apache.commons.httpclient.HttpClient();
var op = new Packages.org.apache.commons.httpclient.methods.PostMethod(tw.local.restSerUrl);

op.setParameter("action","start");
op.setParameter("param1",tw.local.var1);
op.setParameter("param 2",tw.localvar2);
op.setParameter("params",tw.local.JSONparameters);


op.setRequestHeader(new Packages.org.apache.commons.httpclient.Header("Accept","application/xml"));

var auth = new Packages.org.apache.commons.httpclient.UsernamePasswordCredentials("restapiuser","restapipass");
op.setDoAuthentication(true);
client.getState().setCredentials(null, null, auth);

try {
  var statusCode = client.executeMethod(op);
  tw.local.output = op.getResponseBodyAsString();
  tw.local.response =Packages.com.lombardisoftware.core.XMLUtilities.stringToElement(tw.local.output);

} finally {
  op.releaseConnection();
}

parse the status

if(tw.local.response.getElementByTagName("status").item(0).getText()=="200"){
    tw.local.instanceId= tw.local.response.getElementByTagName("data").item(0).getElementByTagName("element").item(0).getText();
}else{
    tw.local.errorMessage=tw.local.response.toString(true);
  //  tw.local.instanceId=tw.local.response.toString(true);
}

Also check the post to perform rest call in IBM with input as Entity.
Hope this post is help full !!!

Thanks for stopping by. If you like this post leave a comment, share it and do a g +1.



2 comments:

  1. Thanks for the post. Can you please provide more details on these 3 statements?

    op.setParameter("param1",tw.local.var1);
    op.setParameter("param 2",tw.localvar2);
    op.setParameter("params",tw.local.JSONparameters);

    What is the type of tw.local.JSONparameters and how does it value look like?

    ReplyDelete
  2. If i perform log.info(tw.local.StatusCode);
    It is logging Status Code 415.Why?
    Iam executing following code.
    var client = new Packages.org.apache.commons.httpclient.HttpClient();
    var credentials = new Packages.org.apache.commons.httpclient.UsernamePasswordCredentials("admin", "admin");
    var scope = new Packages.org.apache.commons.httpclient.auth.AuthScope(tw.local.host,tw.local.port);
    client.getState().setCredentials(scope, credentials);
    var op = new Packages.org.apache.commons.httpclient.methods.PostMethod("http://"+tw.local.host+":"+tw.local.port+"/rest/bpm/wle/v1/processApps");
    op.setRequestHeader(new Packages.org.apache.commons.httpclient.Header("Accept","application/xml"));
    try {
    var statusCode = client.executeMethod(op);
    log.info("status code is"+statusCode);

    } catch(e){
    java.lang.System.out.println("error :: "+e.message);
    }

    ReplyDelete