Urgent: Unable to do achieve the below requirement in javascript

Need to achieve the functionality where initially we have to trigger the backend with pageNumber=0 as queryparam where backend returns response in json and we need to check the field hasNext equal to true then increase the pageNumber in queryparam e.g pageNumber=1. This process should go until hasNext equal to false finally we have to concatenate the response.  
 
Note:  Every time we have to trigger the same endpoint and append the queryparam. pageNumber is starts from 0, goes up to 20 and pageNumber will increase in future. So i have to handle the code now for the increased number. 
 
Getting first response and nothing happening after that. 
 
Below is the code: 
// Function to fetch all pages of data
 
var BackendURL = context.getVariable("BackendURL");
//fetching token from pervious policy 
var accessToken = context.getVariable("access_token");
//Assigning token to the backend
var headers = {"NetWitness-Token": accessToken};
 
 
function CyberSecurityResponseHandler(response,error) {
if (response) {
    if(response.status !=200){
                response=JSON.parse(context.getVariable("response.content"));
context.setVariable('CyberSecurityResponseStatus', response.status);
                context.setVariable('CyberSecurityResponse', response.content);
                throw "error";
            }  else {
            context.setVariable('CyberSecurityResponseStatus', response.status);
                context.setVariable('CyberSecurityResponse', response.content);
}
}
}
var myRequest = new Request(BackendURL,"GET",headers);
var exchangeObj = httpClient.send(myRequest, CyberSecurityResponseHandler);
 
// var exchangeObj = httpClient.send(myRequest, CyberSecurityResponseHandler);
 
 
// Function to recursively fetch data until hasNext is false
function fetchData() {
    // Send the request to the backend service
   var exchangeObj = httpClient.send(myRequest, CyberSecurityResponseHandler, function (httpClientResponse) {
        // Extracting response body
        var responseData = JSON.parse(context.getVariable("response.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();
2 5 137
5 REPLIES 5

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 🙂

 

@chrismca73 , 

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();

Hi @Prashanth4576

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:

Prashanth4576_0-1710657148321.png

 

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.

chrismca73_0-1710666209626.png