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);
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”.
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));