How to handle multiple target calls over https

Hi All,

I have the below requirement, can someone suggest me how to handle?

1. I have to call backend services over https.

2. Upon receiving the response from the call above, another call should me made to other backend services over https, but this time the call should be async.

3. Send the repsonse received from the first call to the client.

0 6 584
6 REPLIES 6

@ravi kumar karlapudi ,

You can use "Service Callout Policy" without response element if you are not worried about response returned.

See sample here that explains async callout using javascript. Hope it helps.

@Anil Sagar

Thanks for your suggestion.

i am rephrasing my question as below:

I need to achieve the following use case in my API proxy:

1) Call backend service A over https(2 way SSL ) and send the response to the client.

2) Call backend service B(only if service A response was a success) over https(2 way SSL ).

The objective is to continue the processing in the API proxy with other policies after the response from service A is sent back to the client.

And another thing is i tried with the node.js below to communicate with the server:

var https = require('http');

var fs = require('fs');

var options = {

key: fs.readFileSync('key.pem'),

cert: fs.readFileSync('cert.pem'),

hostname: 'backendhostname',

port: 8060,

path: '/clearcache',

method: 'GET'

};

var req = https.request(options, (res) => {

console.log('statusCode:', res.statusCode);

console.log('headers:', res.headers);

res.on('data', (d) => { process.stdout.write(d); }); });

req.end(); req.on('error', (e) => { console.error(e); });

But getting error : *** syntax error syntax error at module.js:439 at module.js:474 at module.js:356 at module.js:312 at module.js:497 at startup (trireme.js:142) at trireme.js:923

adas
New Member

If I understood your requirement correctly, you want to send the response back to the client for service A, but not for service B. Service B is only supposed to be called, when Service A is successful and both services are only exposed via 2-way ssl.

If that's the correct understanding, here's my recommendation:

- create an apiproxy that talks to service A over 2-way ssl. There are plenty of examples of how you call a target using 2-way ssl

- create another apiproxy that talks to service B, similar to service A.

- in apiproxy A, have a javascript policy that makes async http callout to proxy B only if response.status.code == 200. The example here shows how to make async javascript http calls.

This should work for your use-case. Let me know, if this helps.

@arghya das Thanks Arghya, this is a perfect solution for my use case.

As i am calling local proxy, i am trying to give localhost:8998 with base path which is not working, if i give complete url of the proxy it is working.

how do i pass dynamic url (specific to my environments) in javascript?

Thanks in advance.

@ravi kumar karlapudi

If your VHOST for port 8998 is secure, then please use https://localhost:8998 and in case its not secure then use http.

Also, can you please try once by replacing localhost with 127.0.0.1

@arghya das i tried your suggesstion above, when i am calling proxy B from javascript - i dont think it is doing async call, because untill the proxy B completed its processing then only the response from proxy A is sent back.