My apologies . I am very new to postman , recently started using this .Please find the pre-request script:
console.log(“entering in the request”);
// Get the values of “runtime” and “actualLoadOnUPS” from the external JSON
const runtime = pm.iterationData.get(“runtime”);
const actualLoadOnUPS = pm.iterationData.get(“actualLoadOnUPS”);
// Create an array to store the responses
let responses = ;
function sendRequest(req) {
return new Promise((resolve, reject) => {
pm.sendRequest(req, (err, res) => {
if (err) {
return reject(err);
}
return resolve(res);
})
});
}
// Create an async function to wrap the code
(async function main() {
try {
// Iterate over the values one by one
for (let i = 0; i < runtime.length; i++) {
// Set the current values in variables
const currentRuntime = runtime[i];
// Set the values in the request body
pm.request.body.update(JSON.stringify({
ups: pm.iterationData.get("ups"),
category: pm.iterationData.get("category"),
upsQuantity: pm.iterationData.get("upsQuantity"),
countryId: pm.iterationData.get("countryId"),
frequency: pm.iterationData.get("frequency"),
family: pm.iterationData.get("family"),
batteryType: pm.iterationData.get("batteryType"),
runtime: currentRuntime,
voltage: pm.iterationData.get("voltage"),
upsCapacity: pm.iterationData.get("upsCapacity"),
redundancy: pm.iterationData.get("redundancy"),
userType: pm.iterationData.get("userType"),
loadPercentage: pm.iterationData.get("loadPercentage"),
actualLoadOnUPS: actualLoadOnUPS,
powerFactor: pm.iterationData.get("powerFactor"),
bypass: pm.iterationData.get("bypass")
}));
// Example with a full-fledged request
const postRequest = {
//assume the request url and header section here ,
},
body: pm.request.body
};
// Make the request and await the response
const response = await sendRequest(postRequest);
// Store the response data in the array
responses.push(response.json());
}
// Set the responses array in an environment or global variable
pm.environment.set("responses", JSON.stringify(responses));
} catch (error) {
console.error(error);
}
})();
external json:
[
{
“ups”: “GVSUPS50KHS”,
“category”: “UPS”,
“upsQuantity”: 1,
“countryId”: “DE”,
“frequency”: “50”,
“family”: “GVS”,
“batteryType”: “modular”,
“runtime”: [23,55,67],
“voltage”: “400_400”,
“upsCapacity”: 50,
“redundancy”: false,
“userType”: “se”,
“loadPercentage”: 100,
“actualLoadOnUPS”: 100,
“powerFactor”: 1.0,
“bypass”: false
}
]
can I only update the specific key of the request body (runtime) through pre-request script so that I do not need to enter my whole request body part in pre-request script ?