Connect to webservice using HTTP POST-trigger

Posted by Jorrit on 25-Feb-2016 03:35

Hi.

can anybody help me?

in Rollbase Privatecloud we want to post to a webservice using the HTTP POST-trigger.

How can i authenticate using header information?

i have tried:

Authorization: Basic (username, password,domain)

thanxs in advance.

Regards Jorrit

Posted by Shiva Duriseati on 10-May-2016 02:03

This has been followed through the Support Case:

The main reason for getting 401 error :- Service(Consumer) is expecting a SOAP message which is to be sent through the "Request Body" as raw data.

As on date Rollbase do not have any API which will take input as request body as raw data. Currenly rollbase has two API's which are used for sending POST requests

1)rbv_api.sendHttpPost-This works fine when there is no request body.
2)rbv_api.sendJSONRequest- This works fine when the request body is been consumer accepts only "application/json" types.

A defect #41365 has been logged for enhancing existing rbv_api.sendHttpPost API to accept one more parameter.

However the request body can still be sent as a raw data using java classes which could exposed to Rollbase javascript.(Applicable only for Private cloud)

Below are the steps for sending HTTP POST request with request body.

1)Add below packages to CustomerClassFilter in shared.properties as below.
CustomClassFilter=(java.net.*)|(java.lang.*)|(java.io.*)|(sun.net.www.protocol.*)|(javax.xml.namespace.*)|(javax.xml.ws.*)|(org.apache.http.*) |(sun.net.www.http.*)

2)Create an object script trigger and copy the below code.(The following code sample assumes request body as "SOAP Message" of type "text/xml")

var obj=new Packages.java.net.URL("localhost:9999/.../hello"); //URI of the deployed service
var con=(obj.openConnection());
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Username", "shiva");
con.setRequestProperty("Password", "password");
con.setRequestProperty("Content-Type", "text/xml");
var os=con.getOutputStream();//Opening output stream since SOAP message needs to be sent as request body as a raw data.
var input="<soapenv:Envelope xmlns:soapenv=\"schemas.xmlsoap.org/.../\" xmlns:ws=\"ws.test.com/.../soapenv:Envelope>";
var utf8 = {};
utf8.toByteArray = function(str) { //This function converts String to byte array
var byteArray = [];
for (var i = 0; i < str.length; i++)
if (str.charCodeAt(i) <= 0x7F)
byteArray.push(str.charCodeAt(i));
else {
var h = encodeURIComponent(str.charAt(i)).substr(1).split('%');
for (var j = 0; j < h.length; j++)
byteArray.push(parseInt(h[j], 16));
}
return byteArray;
};
os.write(utf8.toByteArray(input));//Converting String to array of bytes since write method accepts only array of bytes.
os.flush();
var responseCode = con.getResponseCode();//Optional variable to check if response is ok or failed.
rbv_api.println(responseCode);
var br=new Packages.java.io.BufferedReader(new Packages.java.io.InputStreamReader(con.getInputStream()));
var output;
while((output=br.readLine())!=null){
rbv_api.println(output);
}

Regards,

Shiva

All Replies

Posted by Mani Kumar on 25-Feb-2016 03:40

Hi,

Are you looking for similar information :

community.progress.com/.../17539

-Mani.

Posted by Jorrit on 22-Mar-2016 03:44

Hi Many,

Even with the information in de post you send me i still cant connect to the webservice.

Can you please help me?

The admnistrator of the website send me how they connect to te webservice in .net C# see image attached:

This is my code used from the post above:

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
input = escape(input);
var output = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;
do {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);

enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
keyStr.charAt(enc1) +
keyStr.charAt(enc2) +
keyStr.charAt(enc3) +
keyStr.charAt(enc4);
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);
return output;
}

var username = 'domain\username';
var password = 'password';


var siteAuth = username + ":" + password;

var encodedSiteAuth = encode64(siteAuth);

//rbv_api.println(encodedSiteAuth);

var url = "webservice_URL.....";


var header = { "Authorization” : “Basic " : encodedSiteAuth};
//rbv_api.println(header);

var data = null;

var method = "POST";
var contentType = "application/xml; charset=UTF-8";

var request = rbv_api.sendJSONRequest(url, data, method, contentType, username, password, null);
//rbv_api.println(request);

When i debug the code i still get the error:

HTTP Error 401: HTTP call failed: Unauthorized (line #64)

Thanks in advance,

Regards Jorrit

Posted by Jorrit on 01-Apr-2016 06:08

Hi Mani or anyone,

i still got the problem connecting to a webservice.

regards Jorrit

Posted by Jorrit on 06-Apr-2016 04:28

Anyone?

Posted by Jorrit on 06-Apr-2016 04:29

Hi Mani,

can you please help me with my problem?

regards Jorrit

Posted by Jorrit on 18-Apr-2016 09:56

Anybody?

Posted by Shiva Duriseati on 18-Apr-2016 10:06

Hi Jorrit,

Can you try sending from a generic REST client like POSTMAN and post raw request (password masked) for a success run ?

Regards,

Shiva

Posted by Shiva Duriseati on 18-Apr-2016 10:16

Also, in your code can you please specify the method as ""GET" instead of "POST" ?

Posted by Jorrit on 19-Apr-2016 07:03

Hi Shiva, i already tried to set "GET".

In postman i get the next result:

Www-Authenticate: Negotiate; NTLM

so with basic authentication i cant connect to the service.

Is there any way to connect via NTLM?

thanks for your time and response.

Regards Jorrit

Posted by Shiva Duriseati on 19-Apr-2016 13:07

Hi Jorrit,

Can you please open a support ticket ? this needs a little investigation.

Regards,Shiva

Posted by Shiva Duriseati on 10-May-2016 02:03

This has been followed through the Support Case:

The main reason for getting 401 error :- Service(Consumer) is expecting a SOAP message which is to be sent through the "Request Body" as raw data.

As on date Rollbase do not have any API which will take input as request body as raw data. Currenly rollbase has two API's which are used for sending POST requests

1)rbv_api.sendHttpPost-This works fine when there is no request body.
2)rbv_api.sendJSONRequest- This works fine when the request body is been consumer accepts only "application/json" types.

A defect #41365 has been logged for enhancing existing rbv_api.sendHttpPost API to accept one more parameter.

However the request body can still be sent as a raw data using java classes which could exposed to Rollbase javascript.(Applicable only for Private cloud)

Below are the steps for sending HTTP POST request with request body.

1)Add below packages to CustomerClassFilter in shared.properties as below.
CustomClassFilter=(java.net.*)|(java.lang.*)|(java.io.*)|(sun.net.www.protocol.*)|(javax.xml.namespace.*)|(javax.xml.ws.*)|(org.apache.http.*) |(sun.net.www.http.*)

2)Create an object script trigger and copy the below code.(The following code sample assumes request body as "SOAP Message" of type "text/xml")

var obj=new Packages.java.net.URL("localhost:9999/.../hello"); //URI of the deployed service
var con=(obj.openConnection());
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Username", "shiva");
con.setRequestProperty("Password", "password");
con.setRequestProperty("Content-Type", "text/xml");
var os=con.getOutputStream();//Opening output stream since SOAP message needs to be sent as request body as a raw data.
var input="<soapenv:Envelope xmlns:soapenv=\"schemas.xmlsoap.org/.../\" xmlns:ws=\"ws.test.com/.../soapenv:Envelope>";
var utf8 = {};
utf8.toByteArray = function(str) { //This function converts String to byte array
var byteArray = [];
for (var i = 0; i < str.length; i++)
if (str.charCodeAt(i) <= 0x7F)
byteArray.push(str.charCodeAt(i));
else {
var h = encodeURIComponent(str.charAt(i)).substr(1).split('%');
for (var j = 0; j < h.length; j++)
byteArray.push(parseInt(h[j], 16));
}
return byteArray;
};
os.write(utf8.toByteArray(input));//Converting String to array of bytes since write method accepts only array of bytes.
os.flush();
var responseCode = con.getResponseCode();//Optional variable to check if response is ok or failed.
rbv_api.println(responseCode);
var br=new Packages.java.io.BufferedReader(new Packages.java.io.InputStreamReader(con.getInputStream()));
var output;
while((output=br.readLine())!=null){
rbv_api.println(output);
}

Regards,

Shiva

Posted by Jorrit on 10-May-2016 02:39

Hi Shiva,

Its working!!

Thank you for your time and support!

Regards Jorrit

Posted by Jorrit on 12-Aug-2016 07:00

Hi Shiva,

The code above sometimes gives a error:

Error Cannot convert org.mozilla.javascript.NativeArray@71054893 to byte[]

Do you have any idea what this error contains?

Regards Jorrit

Posted by Shiva Duriseati on 12-Aug-2016 08:29

Hi Jorrit,

Replace var utf8 = {}; with below line

var utf8 = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 8142);

Please let me know the results.

Regards,

Shiva

This thread is closed