//PreRequest:
const moment = require('moment');
const today = moment();
var todate = today.format();
pm.globals.set("toDate", todate);
console.log(todate)
var fromdate = moment().subtract(5, 'days');
pm.globals.set("fromDate", fromdate.format());
console.log('fromDate')
//Main API Get Request.
URL: get https\mydashboard.com\dahboard_id
//Code in Test tab of postman API get request.
//This is to get panels query array which will be used in post call body.
var dashboardData = JSON.parse(responseBody);
const myArray = [];
const q_Array = [];
var completedReq = 0;
var qcount = 0;
for (var i = 0; i < dashboardData.panels.length; i++) {
if (dashboardData.panels[i].queries) {
const querystrng = dashboardData.panels[i].queries[0].queryString
var QVAL = JSON.stringify(querystrng)
var queryval = QVAL.replace(/\n/g, "\n").replace(/{{Site}}/g, "*")
//This is to add each query string into an array list
q_Array.push(queryval)
}
}
//This section is used to get the next post call url and prerequest details
var searchURL = pm.environment.get("srhURL")
let frmDte = pm.globals.get("fromDate")
let toDte = pm.globals.get("toDate")
//This is post call request in the same Test tab of main get call request.
pm.test("Status code is 202", function () {
q_Array.forEach((qv, index) => {
console.log("arrrrr", q_Array[index]);
pm.sendRequest({
url: '${ searchURL } ? from = ${ frmDte } & timeZone=IST & to=${ toDte }',
method: 'POST',
header: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': pm.environment.get("AuthToken")
},
body: {
mode: 'raw',
raw: JSON.stringify({
'username': '******n', 'password': '*******', 'from': pm.globals.get("fromDate"), 'to': pm.globals.get("toDate"), "timeZone": "IST",
"byReceiptTime": true, 'query': qv,
})
}
}, function (error, response) {
if (error) {
console.error("Error in get: ", error);
}
else {
console.log("Getdata: ", response)
pm.expect(response).to.have.property("code", 202);
pm.expect(response).to.have.property("status", "Accepted");
const dbresponse = response.json();
console.log("Mytestdata: ", dbresponse.id);
const ids = dbresponse.id;
myArray[index] = ids;
pm.environment.set("idArray", JSON.stringify(myArray));
}
SecondRequest();
}//post call fun end
);
});
});
//This is next get request in the same main Get request Test tab.
function SecondRequest() {
pm.test('JOB ID calls:', function () {
//Here I am parsing the jobid array list which we got from each post call response and saved as env variable.
const arrID = JSON.parse(pm.environment.get('idArray'));
arrID.forEach((jid) => {
pm.sendRequest({
url: 'https://api.us2.sumologic.com/api/v1/search/jobs/' + jid,
method: 'GET',
header: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
},
(err, res) => {
if (err) {
console.error(err);
}
else {
console.log(res);
const searchidData = res.json();
const warningMsg = searchidData.pendingWarnings[0]
console.log("Warning Messages:", JSON.stringify(warningMsg));
}
}
);
});
});
completedReq++;
console.log('Completed post:', completedReq)
}
Here, I am expecting first main get response execute and it gives the array of query list and that will be used in next post requests body and each post request will run and give the array list of jobid which will be used in the next get call urls and each jid will be appended in the get calls.
This is working and giving us the jid list.
Problem
Get call is running randomly and sometimes it executes within the post call or runs more than the 200 times