Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random value output using Postman

Tags:

json

postman

I am trying to generate an output as a random number using Postman so that I can PUT it onto a 'thing' in my IoT app

If I give the value in the following format, it works correctly:

{

"WindSpeed" : "88" }

But now I want to pass on the value of the "WindSpeed" in an automated manner (something like using the random value function) so that I don't have to manually change it every time,

Unfortunately, I am not able to do so as I have trying ways available online including setting global variables etc. etc. but it is always giving an error of 'BAD STRING' or that the JSON content does not have 'ValidProperties' etc. I think that maybe my syntax is wrong. Could someone please guide me as to how I can generate random values in postman(syntax etc.)?

like image 661
Tushar Avatar asked Jul 28 '26 03:07

Tushar


2 Answers

but why not just to use

postman.setEnvironmentVariable("random_list_name", _.random(1, 10000000))

Where "random_list_name" Environment Variable

This is simple and seems does the same

like image 171
Alexander Tunick Avatar answered Jul 30 '26 21:07

Alexander Tunick


You shall generate your random value in the prescript tab using a function like this one:

// random generator function
function getRandomInt(min, max) {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min)) + min;
}
// generate the random value
const myval = getRandomInt(0,100);
// set the value into the global variable
pm.globals.set('value', myval);

// to see it in console
console.log(myval);

Then, in your JSON body, you shall use it:

{
    "Windspeed":{{value}}
}

This should work.

like image 30
A.Joly Avatar answered Jul 30 '26 21:07

A.Joly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!