I have a request body as follows, Here I need the keys "id", "sys2Acct" and "shortName" to be set to a same value.
NOTE: the request body has huge number of key's, I somehow want to assign the same number (i.e random) to the mentioned keys in this request and also for the other requests in the collection.
Currently I have written the following script in Collection Pre-Request, But the problem is it is being called every time for the keys, hence resulting in different numbers for the keys
Code in collection pre-request script:
pm.globals.set("randomNumber", JSON.stringify('{{$randomCreditCardMask}}'));
Output:
{"id":"0372","name":"AN","contName":"CN","phone":"","sys2Acct":"7491","shortName":"2592"}
Can you please guide me how to achieve this.
"id":{{randomNumber}},"name":"AN","contName":"CN","phone":"","sys2Acct":{{randomNumber}},"shortName":{{randomNumber}},
If I understand your problem correctly and you have a body that on each request needs to be sent where 3 parameters have the same random number, you can do this (just one way of doing it)
On your pre-request script generate a
randomIntwith Postman Dynamic variables. Then, set the whole payload to a global variable:Now set the body to the
{{payload}}And you can see that on each request, the body is the the same with the 3 properties you listed set to the same random number. I've created a public request with this example, so if you hit send and then check the Body response you'll see that the payload works as you described it:
Request #1
Request #2
And so on.