Extra post request is executing even after the loop completes in pre-request script

Extra post request is executing even after the loop completes in pre-request script.

  1. Since I am sending every information though pre-request script , then also after removing the request body from the body section and the request URL , it fails for the last extra post request even after the loops complete
  2. And when I keep everything intact then also one extra post request is happening but not failing.

I tried to set the flag when iteration completes and then try to stop the last extra post request but didn’t work.

(value of runtime.length is 3)
(marked with red is the extra post request)

Please help with this issue.

When posting code (or JSON), please use the preformatted text option in the editor (and paste it in as text not a screenshot). It’s much easier to read that way.

The pre-request tab is just that. It runs before the actual request. You can’t stop it from running the actual request.

The 4th\failing request is the actual request itself. If you try and remove the request body and URL, it will still run (and error). Expand the Post request in the console and you will probably see that the URL and body are empty (proving that its the actual request itself).

The reason it works if you don’t blank the info, is that its will contain the same info as the 3rd request (again, expand the post request in the console to confirm).

Not really sure what you are doing with all of that code. With the promises and await functions. There are various topics on the subject of the asynchronous nature of sendRequest(). I would suggest a forum search. I’ve never really had to worry about asynchronous calls, so I can’t really advise on this apart from to read the previous topics. It can be an issue, and the functionality changed last year so a bit of light reading may be necessary.

I would recommend that you break your code down into steps, and clearly define each step.

You have a variable called actualLoadOnUps that I can’t seen being used anywhere else.

You have a bit of code with pm.body.request.update which also doesn’t seem to do anything. Looks like it changes it to a blank object.

let loopCompleted = false;
Where does this get change to true, I can’t see it referenced anywhere else.

Comment you code into steps and this will be easier to understand and to troubleshoot. (Particularly when asking for help). At the moment, it looks like there are several elements in your code that aren’t actually doing anything.

This is the usual way to update a JSON body in a raw request.

const body = JSON.parse(pm.request.body.raw);
// some manipulation here.
pm.request.body.raw = body;

pm.request.body is a special variable. I have no idea with the implication of changing and using it in a pre-request script is. I suspect you may have scoping issues (and it won’t actually be updated for the pre-request script).

My recommendation for the requests in the pre-request script is to define and set its own body. (Don’t use the main pm.request body). Create your own variable to store the body for the sendRequest(). Removes any potential scoping issues.

If you do use pm.request.body, then I recommend console logging it just before the sendRequest() to ensure that it contains the correct\updated information.

TLDR. Break your code down into clearly defined steps.

1 Like

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
    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

// Set the responses array in an environment or global variable
pm.environment.set("responses", JSON.stringify(responses));

} catch (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 ?

You haven’t used the preformatted option for all of the code, so its still very hard to read.

It looks like your iteration data (external JSON) contains all of the elements for the body.

Therefore you should be able to define the body by retrieving the whole object which is stored in a special “data” variable.

let body = pm.iterationData.get(data);

This will create an object called “body” that has all of the iteration data for the current iteration.

Console log it so that you can confirm the JSON looks ok.

You can then update the runtime using

body.runtime = runtime[i];
// or body.runtime = currentRuntime

Not sure what your are doing with actualLoadOnUPS as that seems like a single number. It doesn’t look like it needs updating. (If it does, use the same process as above).

Use the “body” variable defined above for your sendRequest() instead of pm.request.body.

1 Like