Hi Team,
I have below payload request, from the below payload for-each id I have to make different API calls.
For Suppose In request If I received 3 Id's from the list will make below GET call.
a) https://10.10.9.10:8000/test1/123
b) https://10.10.9.10:8000/test1/456
c) https://10.10.9.10:8000/test1/789
After getting the below success response from each API call, I need to fetch one of the element from the response.
{
data :{
"firstName" :"xxxx",
"Lastname" :"yyyyy"
}
}
Also If above mentioned 3 calls are success and responsecode="200", then agian I have to make PATCH/POST call with new url by sending the payload. If anyone of the "id" response fail,will not make any call to other PATCHApicall.
Here my query, Since I have so many calls are running under urlopen,I want to add this urlopen in code in one function and trying to call that function in same js. But while trying to call the function , how to fetch the response of that particualr, Since I have to
pass one of the element to the next PATCH call. ?
Could you please suggest what is the best approach and how to return the json response from one function and use it in another function with in the same js file.
can we achieve this requirement thru js functions or else please suggest me is there any otherway to approach on this in gatewayscript. If yes, please share some sample code.
Sample code.
Input:
{
"FinalList": {
"IdLst": {
"id": [
"123",
"456",
"789"
]
}
}
}
var id = input.FinalList.IdLst.id;
id.forEach(function(id){
var uri = url + 'test/' +id(123,456,789);
var getapicall = {
target: uri,
sslClientProfile: 'xxx',
method: 'GET',
contentType: 'application/json',
headers: {
'Token': Token
},
};
urlopen.open(getapicall, function(error, response) {
if (error) {
logger.error("urlopen connect error with getapicall" + JSON.stringify(error));
session.reject("urlopen connect error with getapicall");
} else {
var responseStatusCode = response.statusCode;
if (responseStatusCode == 200 ) {
response.readAsBuffer(function(error, responseData) {
f (error) {
//dp:reject stop the transaction and go error rule with error code and description
logger.error("failure in putting message to getapicall" + JSON.stringify(error));
//session.reject("failure in putting message to getapicall");
} else {
hm.response.statusCode = responseStatusCode;
var getapiesponse = JSON.parse(responseData);
var firstName = JSON.parse(responseData).data.firstName;
if(firstName != undefined) {
var patchcallurl = url+'test2/'+id(123,456,789);
var TokenValue = apiToken;
var payload = {
"Names": firstName
}
var PATCHApicall = {
target: patchcallurl,
sslClientProfile: 'xxxx',
method: 'PATCH',
contentType: 'application/json',
headers: {
'Token': Token
},
data: payload
};
// then again from this 200:success response, I have to fetch one of the element for the corresponding Id and and again make call to the other url as like above.
}
}
});
}
});
});
#DataPower#Support#SupportMigration