Change body each iteration in pm.sendRequest

I need to test some functionality x amount of times, let’s say 10 times for this purpose.
Is it possible to change the body in the request that is being sent through pm.sendRequest(), each iteration?

I do have a body in the main request that and I use like this:
const req = {
url: `${host}
method: ‘POST’,
header: {
},
body: {
mode: ‘raw’,
raw: pm.request.body.raw
}
}

And inside the main body-request I use variables that i.e, sets random numbers on some of the fields, like this:

"amount": "{{amount}}"

But when I run the request, the body doesn’t change, so I get the same response 10 times.

Is this even possible through script?

Hello @tosh, are you assigning the Dynamic variable and assigning it to the variable {{amount}} and using it as part of your body?

If amount can be integer, then you can use the below snippet in the pre-request script section and can access the variable {{amount}} in the body.

pm.environment.set("amount", pm.variables.replaceIn('{{$randomInt}}'));

I have declared as environment variable. You can define as per your scope.

Please provide more details/screenshots if you still have some issues :slight_smile:

Hi @bpricilla, thanks you replying.

I have declared the variable amount:
let amount = Math.floor(Math.random() * 99).toFixed(2);
pm.environment.set(‘amount’, amount);

I.e: my body looks like this:

{

“amount”: {{amount}}
}

And I want my pm.sendRequest() to modify this body each iteration, which means, I want the response to be a unique amount each iteration, but when I run it, the same amount returns each iteration. Seems like the body doesn’t change inside my iteration.

my body looks like this (just an example).
body

my prerequisite:
preq

my sendRequest:

When sending this, I expect two different amounts, but the same amount are returned twice.
I have also stripped some stuff so pmtRes.paymentId refers to something else, so dont’t bother look too much at that.

hwy @tosh can you add a

Console.Log("PreReq Accessed");

into the prerequisites tab and run 2 iterations, and check that the above is printed out twice to confirm that it is indeed running the preReq each time?

thanks

Hi @JBird20

You’re correct, prerequisite are only initiated in the first request.
Printed once.

Are we able to bypass that in some sort?

pm.globals.set(“amount”, pm.variables.replaceIn(’{{$randomInt}}’));

In prerequisite gives me a fresh number from 0 - 999 every time.

Are you running this through Runner and keeping “keep variable values” ticked?

Also:

let amount = Math.floor(Math.random() * 99).toFixed(2);

pm.environment.set("amount", pm.variables.replaceIn(amount));

works fine for me also. But I did notice that there was a 0.5ms delay in the value updating, so could you humor me and try:

let amount = Math.floor(Math.random() * 99).toFixed(2);

pm.environment.set("amount", pm.variables.replaceIn(amount));

setTimeout(() => {}, 2000); // 2 second delay

also do you need to save this value in the environment variables? can it just be discarded after use and stick in globals? (if so ill need to update your body to point to globals)

e.g.

let amount = Math.floor(Math.random() * 99).toFixed(2);

pm.globals.set("amount", pm.variables.replaceIn(amount));

@tosh

Actually looking at this with very tired eyes (neary midnight) the issue here is with your code. Specifically the for loop your using.

The PreReq will get a value, you then do a loop to send 2 requests. At no point do you every call the prerequisites code again. This is why you have the same value over and over.

Can I ask why you are doing it this way and not via the runner?

Thanks for your reply @JBird20, the reason for me doing this as a script and not via runner is that we have use newman to initiate the tests.

But we might need to just use the runner for this specific issue.

@tosh

just add the pre req code into your loop to resolve the issue (before let url line) and remove the code from preq tab . Here is an example:

pm.test("POC", function (){

 for(let i=0; i<2; i++){

     let amount = Math.floor(Math.random() * 99).toFixed(2);

     pm.environment.set("amount", pm.variables.replaceIn(amount));

     console.log(amount);

 }

});

image

Let me know if that has resolved it.

Thanks!
It resolved it.

1 Like