Postman Generating an output as a random number

0 votes

So I'm using the Postman so that I can PUT it onto a 'thing' in my IoT app, by generating an output as a random number.

If I use the following format, it works okay:

{

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

But I've failed in doing so. I have been trying ways including but not limited to setting global variables etc. etc. but it throws an error of 'BAD STRING' or that the JSON content does not have 'ValidProperties' etc.

Can anyone please help me out?

Thanks!

Feb 12, 2019 in IoT (Internet of Things) by Shubham
• 13,490 points
6,925 views

1 answer to this question.

0 votes

Try generating 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
var myval = getRandomInt(0,100)
// set the value into the global variable
postman.setGlobalVariable("value",myval)

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

Then, in your JSON body, you can go ahead with the following.

{
    "Windspeed":{{value}}
}

This should work just fine.

answered Feb 12, 2019 by Upasana
• 8,620 points