Hi @Prashanth4576 ,
Not sure what this function is doing function CyberSecurityResponseHandler(response,error)
But to solve your issue, Please replace
var exchangeObj = httpClient.send(myRequest, CyberSecurityResponseHandler, function (httpClientResponse) {
// Extracting response body
var responseData = JSON.parse(context.getVariable("response.content"));
with
var exchangeObj = httpClient.send(myRequest, function (httpClientResponse) {
// Extracting response body
print(httpClientResponse.content)
var responseData = JSON.parse(httpClientResponse.content);
Since you've already made inline handler function for the response, there is no need for CyberSecurityResponseHandler I believe.
httpClientResponse - try using this object throughout the flow. Your code will work as expected.
Good luck 🙂
Thank you so much for the response.
I have rectified the code but the queryparam "pageNumber " is not increasing and not appending to the url.
Let me know do i need to do any changes:
function fetchData() {
// Send the request to the backend service
var exchangeObj = httpClient.send(myRequest, function (httpClientResponse) {
// Extracting response body
var responseData = JSON.parse(httpClientResponse.content);
try {
// Parsing response body as JSON
// var responseData = JSON.parse(responseBody);
// Concatenate the current response data with the existing data
allResponses = allResponses.concat(responseData.data);
// Check if the response contains the 'hasNext' field and it is true
if (responseData.hasOwnProperty('hasNext') && responseData.hasNext === true) {
// Extracting existing pageNumber from query parameter
var pageNumber = parseInt(request.queryParams.pageNumber || 0); // Assuming default pageNumber is 1
// Incrementing pageNumber
pageNumber++;
// Update the query parameter with the incremented pageNumber
exchangeObj.request.pageNumber = pageNumber.toString();
// Call the fetchData function recursively to fetch next page
fetchData();
} else {
// If hasNext is false, set the final response content to the concatenated data
response.content = JSON.stringify(allResponses);
// Send the final response back to the client
sendResponse(response.content);
}
}
catch (error) {
// Log any errors encountered while parsing JSON
// console.error("Error parsing JSON response: ", error);
// Send an error response back to the client
// response.content = JSON.stringify({ error: "Error parsing JSON response" });
// sendResponse(response.content);
}
});
}
// Initial array to hold all responses
var allResponses = [];
// Call the fetchData function to start fetching data
fetchData();
Try adding the required query parameter to the request Object "myRequest". Try this simple workaround to solve you case,
//Initial Page number = 1
var pageNumber = 1;
function fetchData() {
//BackendURL without queryParam;
var BackendURL = context.getVariable("BackendURL");
//Adding query param to URL
BackendURL += "?pageNumber="+pageNumber;
var myRequest = new Request(BackendURL,"GET",headers);
// Send the request to the backend service
var exchangeObj = httpClient.send(myRequest, function (httpClientResponse) {
// Extracting response body
var responseData = JSON.parse(httpClientResponse.content);
try {
// Parsing response body as JSON
// var responseData = JSON.parse(responseBody);
// Concatenate the current response data with the existing data
allResponses = allResponses.concat(responseData.data);
// Check if the response contains the 'hasNext' field and it is true
if (responseData.hasOwnProperty('hasNext') && responseData.hasNext === true) {
// Extracting existing pageNumber from query parameter
//var pageNumber = parseInt(request.queryParams.pageNumber || 0); // Assuming default pageNumber is 1
// Incrementing pageNumber
//pageNumber++;
// Update the query parameter with the incremented pageNumber
pageNumber += 1;
//exchangeObj.request.pageNumber = pageNumber.toString();
// Call the fetchData function recursively to fetch next page
fetchData();
} else {
// If hasNext is false, set the final response content to the concatenated data
response.content = JSON.stringify(allResponses);
// Send the final response back to the client
sendResponse(response.content);
}
}
catch (error) {
// Log any errors encountered while parsing JSON
// console.error("Error parsing JSON response: ", error);
// Send an error response back to the client
// response.content = JSON.stringify({ error: "Error parsing JSON response" });
// sendResponse(response.content);
}
});
}
// Initial array to hold all responses
var allResponses = [];
// Call the fetchData function to start fetching data
fetchData();
Dear @chrismca73 ,
For the above code i am not getting initial response also, eventually function is not executing.
Now i have changed code and wrote in two different javacripts "FetchCompleteResponse" and "CheckfieldStatus"
FetchCompleteResponse code:
var BackendURL = "https://google.com/sample/api/general?since=2024-01-01T14:00:00.000Z&until=2099-01-01T14:00:00.000Z";
var accessToken = context.getVariable("access_token");
var headers = {"NetWitness-Token": accessToken};
function CyberSecurityResponseHandler(response,error) {
if (response) {
var CyberSecurityResponse = response.content;
context.setVariable("responseData",CyberSecurityResponse);
}
}
var myRequest = new Request(BackendURL,"GET",headers);
var exchangeObj = httpClient.send(myRequest, CyberSecurityResponseHandler);
context.setVariable("uri",BackendURL);
CheckfieldStatus Code:
var response = context.getVariable("responseData");
print(response);
var BackendUri = context.getVariable("uri");
var accessToken = context.getVariable("access_token");
var headers = {"NetWitness-Token": accessToken};
var pagenumber = 0;
if (response.hasNext = true) {
print("has hasNext is true");
pagenumber++;
BackendURL = BackendUri + "&pagenumber=" + pagenumber;
print("url after appending", BackendURL);
}
// context.setVariable("request.uri",BackendURL);
var myRequest = new Request(BackendURL,"GET",headers);
var exchangeObj = httpClient.send(myRequest);
Snapshot from trace session where queryparam is increasing but unable to hit backend endpoint:
Hi @Prashanth4576 ,
Unfortunately I'm unable to debug the code which you provided. But I tried this single JavaScript with the condition to call the target API 10 times(pageNumber<=10). It is working as expect. Just try rechecking your response handling & parsing logics.
//Initial Page number = 1
var pageNumber = 1;
function fetchData() {
print("called fetchData")
//BackendURL without queryParam;
// var BackendURL = context.getVariable("BackendURL");
var BackendURL = "https://xxx.yyy.zz/v1/getDetails"
var headers = {"NetWitness-Token": "SomeToken"};
//Adding query param to URL
BackendURL += "?pageNumber="+pageNumber;
var myRequest = new Request(BackendURL,"GET",headers);
// Send the request to the backend service
var exchangeObj = httpClient.send(myRequest, function (httpClientResponse) {
// Extracting response body
var responseData = JSON.parse(httpClientResponse.content);
try {
// Parsing response body as JSON
// var responseData = JSON.parse(responseBody);
// Concatenate the current response data with the existing data
allResponses = allResponses.concat(responseData.data);
// Check if the response contains the 'hasNext' field and it is true
if (pageNumber<=10) {
// Extracting existing pageNumber from query parameter
//var pageNumber = parseInt(request.queryParams.pageNumber || 0); // Assuming default pageNumber is 1
// Incrementing pageNumber
//pageNumber++;
// Update the query parameter with the incremented pageNumber
pageNumber += 1;
//exchangeObj.request.pageNumber = pageNumber.toString();
// Call the fetchData function recursively to fetch next page
fetchData();
} else {
// If hasNext is false, set the final response content to the concatenated data
// response.content = JSON.stringify(allResponses);
// Send the final response back to the client
// sendResponse(response.content);
}
}
catch (error) {
// Log any errors encountered while parsing JSON
// console.error("Error parsing JSON response: ", error);
// Send an error response back to the client
// response.content = JSON.stringify({ error: "Error parsing JSON response" });
// sendResponse(response.content);
}
});
}
// Initial array to hold all responses
var allResponses = [];
// Call the fetchData function to start fetching data
fetchData();
Below is the working screenshot, exactly calling 10 times.