postman - how to generate random number in specifi

2020-07-02 09:32发布

I need to generate random number in specific range for postman. I wonder if there is possible to generate it and to use it in variable in postman, from what I saw at postman site is:

{{$randomint }}

will give random number between 1-1000 and I already tried to do something like this:

{{$randomint(1,5)}}

to get number between 1-5 but postman didn't get this kind of option, so how to specify range for the random in postman?

2条回答
干净又极端
2楼-- · 2020-07-02 10:01

You can just use Lodash for this as it's a built-in module:

pm.environment.set("random_number", _.random(1, 5))

Or just add the number 5 to the _.random() function and this will be a number from 0 to 5.

查看更多
别忘想泡老子
3楼-- · 2020-07-02 10:09

This worked for me:

In your Pre-request script define your variable with:

pm.globals.set('randomNumber', Math.floor(Math.random() * 5));

Then in your URL call your variable in the URL like so:

{{randomNumber}}

Hope this works for you.

查看更多
登录 后发表回答