Skip to Content
0
Jul 24, 2020 at 10:28 AM

Http call with groovy script in CPI

1149 Views Last edit Jul 24, 2020 at 12:39 PM 2 rev

Hi,

I try to connect from iFlow A to iFlow B with a groovy-script.

Although URL, myuserID & myPassword are correct in the script, I get always a 401 response code!
Can anyone help me out here?

Thanks.

See code below:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import org.apache.camel.*;
import groovy.json.JsonSlurper;

def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String;
def messageLog = messageLogFactory.getMessageLog(message);

// Request
def get = new URL("https://<tenant>-iflmap.hcisbp.eu1.hana.ondemand.com/http/dummy/http").openConnection() as HttpURLConnection;

get.setRequestMethod('GET');
get.setRequestProperty("Accept", 'application/json');
get.setRequestProperty("Content-Type", 'application/json');

get.setRequestProperty('Authorization','Basic' + '<myUserID>:<myPassword>'.bytes.encodeBase64().toString());

get.connect();

def getRC = get.getResponseCode();
if(getRC.equals(200)) {
messageLog.setStringProperty( "ResponsePayload:" , getRC.toString() );
}

message.setBody(getRC);
return message;
}