Not able to do Async call to HTTPS URL using JavaScript Policy

Not applicable

Hi,

Based on the value of 'proxy.pathsuffix' I need to invoke HTTPS back end asynchronously to send the JSON Request. I am trying to do so by using a JavaScript Policy.

Along with the payload I need to send the headers also while invoking the HTTPS URL.

I am trying to use httpCilent() but not able to invoke the HTTPS URL.

Please suggest if we can use httpCient() to invoke HTTPS URL. Below is the javascript I have written for this and please correct me if I have done anything wrong in the script.

var jsonRequest = JSON.parse(context.getVariable("jsonRequestMessage")); 
var pathSuffix = context.getVariable("proxy.pathsuffix");
if(pathSuffix == "/search"){ 
var myRequest = new Request("https://target1host.com/mysearch/search","POST",{ 'Header1': context.getVariable("request.header.Header1"), 'Header2':context.getVariable("request.header.Header2"), 'Header3': context.getVariable("request.header.Header3"), 'Header4': context.getVariable("request.header.Header4")},JSON.stringify(jsonRequest));
}
else if(pathSuffix == "/match"){ 
var myRequest = new Request("https://target2host.com/mymatch/match","POST",{ 
'Header1': context.getVariable("request.header.Header1"), 
'Header2':context.getVariable("request.header.Header2"),
'Header3': context.getVariable("request.header.Header3"), 
'Header4': context.getVariable("request.header.Header4") },JSON.stringify(jsonRequest)); }

// asynchronous POST for performance reasons 
var myResult = httpClient.send(myRequest); 

// Wait for the asynchronous POST request to finish
 myResult.waitForComplete();

// get the response object from the exchange 
var responsePayload = myResult.getResponse().content;

// expose the response as a flow variable context.setVariable("MyCalloutResponse",responsePayload); 

Regards

Ravi

0 12 1,540
12 REPLIES 12

Hi @Ravi Kumbar,

I could implement async call from javascript using the same syntax as mentioned above Please could you check the following.

  • if the service call is working from postman
  • if the variables are being read and assigned properly
  • check if myResult.isSucess() or myResult.isError()

Hi @Aswin Segu,

I included the below lines of code in the script above, Please let me know if I can do this.

if(myResult.isSuccess()) {
var responseObj = myResult.getResponse().content.asJSON;
if(responseObj.error) {
throw new Error(responseObj.error_description);}
return responseObj;
}else if(myResult.isError()) {
throw new Error(myResult.getError());
}

Yes, you can do that. But did you check the above points first?

Hi @Aswin Segu,

I am able to test the service call in postman and I am getting the response from the backend in postman. But Not able to through javascript in the proxy yet. In the trace, value of the variable "MyCalloutResponse" I could see as "RemoteExceptionData".

AFAIK, we can't test JavaScript in Postman.

Since you are getting response from backend through postman, try replicating the same on javascript and it will work as it does for all of my backend calls.

Please verify your syntax, variables names etc., while implementing and try to debug the same

I have removed the condition to check for path suffix and just have javascript like below,but still no response from backend being captured in 'MycalloutResponse' variable. Please suggest because I verified for the syntax and variable names I don't see any fault.

Can you please share any of your sample javascript for this kind.

var jsonRequest = JSON.parse(context.getVariable("jsonRequestMessage"));

var Header1=  context.getVariable("request.header.Header1");
 var Header2= context.getVariable("request.header.Header2");
 var Header3 = context.getVariable("request.header.Header3");
 var Header4 = context.getVariable("request.header.Header4");

var myRequest = new Request("https://targethost/match","POST",{
    'Header1': Header1,
    'Header2': Header2,
    'Header3': Header3,
    'Header4': Header4
},JSON.stringify(jsonRequest));

var myResult = httpClient.send(myRequest);

 myResult.waitForComplete();

var responsePayload =  myResult.getResponse().content;

context.setVariable("MycalloutResponse",JSON.stringify(responsePayload));

Please find below my Javascript code for similar scenario

var accessToken = context.getVariable("oauthtoken");
var reportDetailUrl = context.getVariable("reportDetailUrl");
var reportEntryDetailUrl = context.getVariable("reportEntryDetailUrl");


var myRequest = new Request();
myRequest.headers = {'Authorization': 'OAuth ' + accessToken};


if (reportDetailUrl !== "" && reportDetailUrl !== null) {
    myRequest.url = reportDetailUrl;
}
else if (reportEntryDetailUrl !== "" && reportEntryDetailUrl !== null) {
    myRequest.url = reportEntryDetailUrl;
}


var exchange = httpClient.send(myRequest);
exchange.waitForComplete(1000);
if (exchange.isSuccess()) {
	var resp = exchange.getResponse();


	// check the status code
	if (resp.status == 200) {
	  context.setVariable("finalResponse", resp.content.asXML);
	  println(resp.content.asXML);
    } else {
		throw "Failed to connect. Status code is " + resp.status;
    }
} else {
	throw "Failed to connect. Error is {" + exchange.getError() + "} ";
}

Hi @Aswin Segu,

Thanks for sharing the js. I have modified my js as per yours as below,

var jsonRequest = context.getVariable("jsonRequestMessage");
context.setVariable("jsonRequestMsg", jsonRequest);
context.setVariable("AsyncTargetURL","https://target1.com/mysearch/search");
var AsyncSearchTargetURL = context.getVariable("AsyncTargetURL");
var securityToken = context.getVariable("request.header.Authorization"); var systemID = context.getVariable("request.header.System-ID");
var clientID = context.getVariable("request.header.Client-ID");
var correlationID = context.getVariable("request.header.Correlation-ID");
var myRequest = new Request();
myRequest.headers = {'Security-Token': securityToken, 'System-ID': systemID, 'Client-ID': clientID, 'Correlation-ID': correlationID};
myRequest.url = AsyncSearchTargetURL; myRequest.body = JSON.stringify((jsonRequest);
var exchange = httpClient.send(myRequest);
exchange.waitForComplete(1000);
if (exchange.isSuccess()) {
var resp = exchange.getResponse();
context.setVariable("finalResponse", resp.content.asJSON);
} else {
var resp = exchange.getError();
context.setVariable("finalResponse", resp.content.asJSON);
}

Since I need to pass the JSON Payload along with the headers I am doing "myRequest.body = JSON.stringify((jsonRequest)" in the above code.

After these changes also I am not able to invoke the HTTPS backend.

Is this approach fine or any suggestions?


Not applicable

Hi Ravi,

Did you get the answer for the above query,because even I have same requirment for me.

If you have got the answers please do post here.

Thanks in advance.

Hi @sumiya

Check this link . This might help

Not applicable

Hi @snehal chakraborty

This link contains how to call a url with GET request.

But in my case it is POST call where I have to add headers and payload to the request.

@sumiya , Answers are strictly for answers. Please use comments for replies, feedback, queries etc. Thank you.