I have to execute a POST to retrieve all associated data with unique ID "queueCallManagerID'. I have an array of queueCallManagerIDs in my queueCallManagerID environment. How do I build a loop to execute the request for each queueCallManagerID value within the environment.
Environment with required values
I am able to manually generate the expected response, but I have less experience with loops in postman.
Expected response but for all queueCallManagerIDs
I will have to parse through the result to organize a list of mixmonFileName.
This is the farthest I've gotten via pre-script, but it seems that it's not reading the values as an array. Error: "TypeError: queueCallManagerID.forEach is not a function"
// Define authorization keys
const cKey1 = '12345;
const cKey2 = '6789';
const uKey = '8765';
// Retrieve the array of queueCallManagerIDs from the environment
const queueCallManagerID = pm.environment.get('queueCallManagerID');
// Loop through each queueCallManagerID value and send a POST request
queueCallManagerID.forEach(queueCallManagerID => {
const requestBody = {
"cKey1": cKey1,
"cKey2": cKey2,
"uKey": uKey,
"queueCallManagerID": queueCallManagerID
};
// Send the POST request
pm.sendRequest({
url: 'https://api.fathomvoice.com/V2/queues/getInteraction/',
method: 'POST',
header: {
'Content-Type': 'application/json'
},
body: {
mode: 'raw',
raw: JSON.stringify(requestBody)
}
}, function(err, res) {
if (err) {
console.error(err);
return;
}
console.log(res);
console.log("queueCallManagerID:", queueCallManagerID);
});
});
Postman's Run Collection
and shift()
and setNextRequest()
JavaScript commands will address your loop call problem.
pm.environment.set("queueCallManagerID", ids.shift())
postman.setNextRequest("Get Interaction A");
setNextRequest's parameter "Get Interaction A" should be match collection's request name
Save as server.js
const express = require('express');
const fs = require('fs');
const multer = require('multer')
const cors = require('cors');
const app = express();
// for form-data
const forms = multer();
app.use(forms.array());
app.use(cors());
const PORT = 3000;
// Function to generate random 5-digit number
function generateRandomNumber() {
return Math.floor(10000 + Math.random() * 90000);
}
// Define POST endpoint for queueCallManagerIDs Array
app.post('/V2/queues/data', (req, res) => {
fs.readFile('data.json', 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
res.status(500).send('Internal Server Error');
return;
}
try {
const jsonData = JSON.parse(data);
res.json(jsonData);
} catch (error) {
console.error('Error parsing JSON:', error);
res.status(500).send('Internal Server Error');
}
});
});
// Define POST endpoint for getInteraction, support form-data
app.post('/V2/queues/getInteraction', (req, res) => {
const { cKey1, cKey2, uKey, queueCallManagerID } = req.body;
// Form data print
console.log(`cKey1: ${cKey1}`);
console.log(`cKey2: ${cKey2}`);
console.log(`uKey: ${uKey}`);
console.log(`queueCallManagerID: ${queueCallManagerID}`);
fs.readFile('data-result.json', 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
res.status(500).send('Internal Server Error');
return;
}
try {
const jsonData = JSON.parse(data);
// Generate random number
const randomNumber = generateRandomNumber();
jsonData.data.queueCDR.mixmonFileName = jsonData.data.queueCDR.mixmonFileName.replace(/\d{5}\.wav$/, randomNumber + '.wav');
jsonData.data.queueCDR.queueCallManagerID = queueCallManagerID;
res.json(jsonData);
} catch (error) {
console.error('Error parsing JSON:', error);
res.status(500).send('Internal Server Error');
}
});
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Save data.json
{
"count": 3386,
"status": "Complete",
"description": "",
"data": [
{
"endTime": "2024-02-23 05:07:52",
"queueCallManagerID": "79768757",
"mixmonFileName": "con2526-797309501.wav"
},
{
"endTime": "2024-02-23 05:08:18",
"queueCallManagerID": "79766443",
"mixmonFileName": "q-con2526-us1-vp-4008-1708664701.1269429.wav"
},
{
"endTime": "2024-02-23 05:09:52",
"queueCallManagerID": "79767811",
"mixmonFileName": "con2526-797309502.wav"
},
{
"endTime": "2024-02-23 05:10:18",
"queueCallManagerID": "79761324",
"mixmonFileName": "q-con2526-us1-vp-4008-1708664701.1234.wav"
}
]
}
Save data-result.json
{
"status": "Complete",
"description": "",
"data": {
"queueCDR": {
"uKey": "1234",
"queueCDRID": "79566979",
"queueCallManagerID": "79566979",
"queueID": "9576",
"vbxName": "some name",
"channel": "some channel",
"uniqueID": "us1-vp-4055-1708356420.43542",
"mixmonFileName": "q-con2526-us1-vp-4055-43542.wav"
}
}
}
npm install express fs multer cors
After install you can see those files
If not yet install node.js
Try to install node.js
Then resume install dependencies
node server.js
Save as 1-1-demo.postman_collection.json
{
"info": {
"_postman_id": "d65b7cc6-5f59-4d45-86d7-f20439702f53",
"name": "1-1-demo",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "1826150"
},
"item": [
{
"name": "Get queueCallManagerIDs",
"event": [
{
"listen": "prerequest",
"script": {
"exec": [
""
],
"type": "text/javascript"
}
},
{
"listen": "test",
"script": {
"exec": [
"const jsonData = pm.response.json();\r",
"const queueCallManagerIDs = jsonData.data.map(item => item.queueCallManagerID);\r",
"pm.environment.set('queueCallManagerIDs', JSON.stringify(queueCallManagerIDs));\r",
"console.log(\"all data: \" + pm.environment.get(\"queueCallManagerIDs\"));\r",
""
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [],
"url": {
"raw": "http://localhost:3000/V2/queues/data",
"protocol": "http",
"host": [
"localhost"
],
"port": "3000",
"path": [
"V2",
"queues",
"data"
]
}
},
"response": []
},
{
"name": "Get Interaction A",
"event": [
{
"listen": "prerequest",
"script": {
"exec": [
"let ids = JSON.parse(pm.environment.get(\"queueCallManagerIDs\"));\r",
"console.log(ids);\r",
"\r",
"pm.environment.set(\"queueCallManagerID\", ids.shift())\r",
"\r",
"pm.environment.set(\"queueCallManagerIDs\", JSON.stringify(ids));\r",
""
],
"type": "text/javascript"
}
},
{
"listen": "test",
"script": {
"exec": [
"const jsonData = pm.response.json();\r",
"\r",
"console.log(\"queueCallManagerID: \" + pm.environment.get(\"queueCallManagerID\"));\r",
"console.log(\"mixmonFileName: \" + jsonData.data.queueCDR.mixmonFileName);\r",
"\r",
"let ids = JSON.parse(pm.environment.get(\"queueCallManagerIDs\"));\r",
"if (ids.length > 0){\r",
" postman.setNextRequest(\"Get Interaction A\");\r",
"}\r",
"\r",
"const mixmonFileName = jsonData.data.queueCDR.mixmonFileName;\r",
"\r",
"pm.test('mixmonFileName length is greater than 0', function () {\r",
" pm.expect(mixmonFileName.length).to.be.above(0);\r",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "cKey1",
"value": "Key 1 - top secret 6789",
"type": "text"
},
{
"key": "cKey2",
"value": "key 2 - second key 6789",
"type": "text"
},
{
"key": "uKey",
"value": "uKey - third key 8765",
"type": "text"
},
{
"key": "queueCallManagerID",
"value": "{{queueCallManagerID}}",
"type": "text"
}
]
},
"url": {
"raw": "http://localhost:3000/V2/queues/getInteraction",
"protocol": "http",
"host": [
"localhost"
],
"port": "3000",
"path": [
"V2",
"queues",
"getInteraction"
]
}
},
"response": []
}
]
}
Import
Open 1-1-demo.postman_collection.json
POST http://localhost:3000/V2/queues/data
Tests
Tab
const jsonData = pm.response.json();
const queueCallManagerIDs = jsonData.data.map(item => item.queueCallManagerID);
pm.environment.set('queueCallManagerIDs', JSON.stringify(queueCallManagerIDs));
console.log("all data: " + pm.environment.get("queueCallManagerIDs"));
POST http://localhost:3000/V2/queues/getInteraction
Pre-request Script
Tabs
let ids = JSON.parse(pm.environment.get("queueCallManagerIDs"));
console.log(ids);
pm.environment.set("queueCallManagerID", ids.shift())
pm.environment.set("queueCallManagerIDs", JSON.stringify(ids));
Body
Tabs
cKey1 : Key 1 - top secret 6789
cKey2 : key 2 - second key 6789
uKey : uKey - third key 8765
queueCallManagerID : {{queueCallManagerID}}
Tests
Tabs
const jsonData = pm.response.json();
console.log("queueCallManagerID: " + pm.environment.get("queueCallManagerID"));
console.log("mixmonFileName: " + jsonData.data.queueCDR.mixmonFileName);
let ids = JSON.parse(pm.environment.get("queueCallManagerIDs"));
if (ids.length > 0){
postman.setNextRequest("Get Interaction A");
}
const mixmonFileName = jsonData.data.queueCDR.mixmonFileName;
pm.test('mixmonFileName length is greater than 0', function () {
pm.expect(mixmonFileName.length).to.be.above(0);
});
demo
name (any name is fine)Two keys will be create after running collection
This POST called by number queueCallManagerIDs
array.
http://localhost:3000/V2/queues/getInteraction
Different queueCallManagerID
form-data input