Skip to Content
Technical Articles
Author's profile photo Rajath Kodandaramu

CPI http calls from groovy

The design of the IFlows may sometimes demand we make a request in message mapping and get a value which is need for the target system.

We might go of request reply also, but we need to have content modifier (if the source has multiple values to query we might have to use iterator ) and to again if the URL is returning the value needed in XML or JSON we may have to parse again with script.

So , instead we can do everything inside the custom function where we parse the xml and call the URL and get response and replace it in xml or put it in an hashMap and set the hashMap as exchange properties for further use.

If we do not have further use we could just call the URL and replace it the custom mapping function as shown in second code snippet.

When we are trying save key,values in hash map and use during mapping. We can do the below.

Custom Function for making external http calls

CODE

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;

def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String;
def messageLog = messageLogFactory.getMessageLog(message);
def oException = message.getProperty(‘CamelExceptionCaught’);

def get = new URL(“Your URL”).openConnection();
get.setRequestProperty(‘Authorization’, ‘Basic ‘ + ‘Username:password’.bytes.encodeBase64().toString())
def getRC = get.getResponseCode();

if(getRC.equals(200)) {
messageLog.setStringProperty(“ResponsePayload-:”,getRC.toString());
}

def feed = new XmlSlurper().parseText(get.getInputStream().getText())
def uuid = feed.entry.content.properties.MarketingOfferContentUUID[0]
def hashMap = [:]
hashMap.put(OfferIdExt,uuid)

// creating the MAP in external properties

message.setProperty(“testmap”,hashMap)
return message;
}

 

Custom Mapping function

import com.sap.it.api.mapping.*;

//here we are getting the product-id while we are passing its bar-code.

def String GetProductID(String barcode){

def get = new URL(“your URL “+barcode,’UTF-8′)).openConnection();

get.setRequestProperty(‘Authorization’, ‘Basic ‘ + ‘testUser:Password’.bytes.encodeBase64().toString())
def getRC = get.getResponseCode();

def feed = new XmlSlurper().parseText(get.getInputStream().getText())
barcode = feed.entry.content.properties.ProductID[0]

return barcode

}

 

Custom Mapping function with access to value-mapping

import com.sap.it.api.mapping.*;

//here we are getting the mapped value.

def countryValue= getMappedValue(“sourceAgency”,”sourceIdentifier”, “sourceValue”, “targetAgency”, “targetIdentifier”)

def String GetProductID(String barcode){

def get = new URL(“your URL “+barcode,’UTF-8′)).openConnection();

get.setRequestProperty(‘Authorization’, ‘Basic ‘ + countryValue+barcode ‘testUser:Password’.bytes.encodeBase64().toString())
def getRC = get.getResponseCode();

def feed = new XmlSlurper().parseText(get.getInputStream().getText())

barcode = feed.entry.content.properties.ProductID[0]

//returning  appended value

return barcode

}

 

The above would reduce the complexity of the Iflow and could be used based on the context of our requirement. This will be handy if we are querying something on the fly during execution of iflow without adding many steps to Iflow.

 

Assigned Tags

      7 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Michael Davison
      Michael Davison

      I heard that Groovy has a built-in REST/HTTP client. The only library I can find is HTTP Builder, is this it?

      Basically, I'm looking for a way to do HTTP GETs from inside Groovy code without having to import any libraries (if at all possible). But since this module doesn't appear to be a part of core Groovy I'm not sure if I have the right lib here.

       

      Thanks & Regards

      Ask For Outlook Help

      Author's profile photo Rajath Kodandaramu
      Rajath Kodandaramu
      Blog Post Author

      Hello Michael,

      The Groovy runs on JAVA platform, so native java libraries can be used in groovy without importing any external libraries.

      The above URL object is from Java standard API.

      https://docs.oracle.com/javase/7/docs/api/java/net/package-summary.html

      And in CPI if you want you can also use external libraries, but i have not tested it myself.

       

      Hopefully this answers your question.

      Regards,

      Rajath Kodandaramu

      Author's profile photo Heiko Samlowski
      Heiko Samlowski

      Hi Rajath,

       

      I used a similar logic in a mapping step. You have to be careful with those connections... I'm not 100% sure but I think those connections are not closed automatically and can result in an exceedence of the CPI's maximum number of concurrent connections. You should better disconnect the connection after reading the response!

       

      Regards

      Author's profile photo srishti kumari
      srishti kumari

      Hi Rajath,

      I wanted to know if we can use XMLHttpRequest instead of CamelHttpRequest?

      Plus i wanted to  perform the same function using javascript in CPI . I want to make a HTTP  call using a standalone javascript code.What class do i need to import for it.

       

      Thanks

      Srishti

      Author's profile photo Rajath Kodandaramu
      Rajath Kodandaramu
      Blog Post Author

      Hello Srishti,

       

      You can add a jar, and import the object XMLHttpRequest.

      https://mvnrepository.com/artifact/net.jangaroo/xmlhttprequest/1.0.2

      and use importClass(net.jangaroo.xmlhttprequest

      However, good way would be to prepare the request in a script and use https adapter to make the call as error handling and visibility would be better.

      Can you let me know what is the use case for using XMLHttpRequest object ?

      Hope this helps.

      Regards,

      Rajath Kodandaramu

       

      Author's profile photo srishti kumari
      srishti kumari

      Hi Rajath,

      I was just trying  to check this http call thing from Javascript and check if its possible or not.There is no such use case or something.Thank you for your reply

       

      Thanks

      Srishti

      Author's profile photo Erik De Meyer
      Erik De Meyer

      Hi Rajath,

      I tried your script but get an error in following line:

      get.setRequestProperty(‘Authorization’, ‘Basic ‘ + ‘Username:password’.bytes.encodeBase64().toString())

      script3__Script.groovy: 28: illegal colon after argument expression;

      Regards,

      Erik