How to call an external API from Js policy ?

Not applicable

Hi, I'm a beginner in APIGEE. I need your suggestions to make an external API/webservice from a Js policy that I have created. Please help me on this.

I have created a conditional flow and retried its result in its attached JS Policy.From this JS I need to call another API call.Following is my code.

var responseData = JSON.parse(response);
if (responseData.Event.Resource.Id) {
    downloadDataId = responseData.Event.Resource.Id;
    context.setVariable('downloadDataId', downloadDataId);
    var authHeader = context.getVariable('shareFiles.auth_token');
    authHeader = 'Bearer ' + authHeader;
    var url = 'https://my-download-url/Items(' + downloadDataId + ')/Download';
    var headers = {
        'Content-Type': 'application/json',
        'Authorization': authHeader
    };
    var myRequest = new Request(url,"GET",headers);
    try {
        var exchange = httpClient.send(myRequest);
        context.session['exchange'] = exchange;
        var exchange = context.session['exchange'];
        exchange.waitForComplete(1000);
        print(JSON.parse(exchange));
    }
    catch(err) {
        print(err.message);
    }
} else {
    context.setVariable('downloadDataId', '');
}

Following is my response

7250-apigee.png

Please suggest me the right method for solving this issue.

0 5 1,868
5 REPLIES 5

Ok, Can we create dynamic end points in service-call-out ?? (Because my end point contains a dynamic parameter. Ex: 'https://my-download-url/Items('+ downloadDataId +')/Download')

Yes, SC policy supports dynamic variables.

How will you get the downloadDataID? Is it coming in the request?

Its coming as the response of another API.From the first API response I will get the downloadDataID,then I need to call another API with this downloadDataID.

I have also tried this

 req = newRequest(url,'GET', headers),
exchange = httpClient.send(req),
response, status;
exchange.waitForComplete();

But always execute the error condition

elseif(exchange.isError()){thrownewError(exchange.getError());}

Showing error message as 'timed out'