S
Shiva Duriseati
Guest
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=" "; 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
Continue reading...
Continue reading...