1

there are certain discussions on generating random stuff within a range on stack but none of them answer the actual question. So task is the following

Need to generate a random number within a range, let's say from 1 to 13010, and put this number into the body. example

{
   "userid": {{randomNumberFromRange}},
   "date": "{{RandomDateInFuture}}"
}

the main requirement - this should work in POST request body. Any ideas?

lucas-nguyen-17
  • 5,516
  • 2
  • 9
  • 20
soda432
  • 11
  • 3

1 Answers1

1

In Pre-request:

pm.variables.set("randomNumberFromRange", _.random(1, 13010));

function randomDate(start, end) {
  return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()))
}

pm.variables.set("RandomDateInFuture", randomDate(new Date(), new Date(2030, 0, 1)));

Result:

{
   "userid": 3538,
   "date": "2023-07-19T22:17:13.379Z"
}

Reference: https://gist.github.com/miguelmota/5b67e03845d840c949c4

lucas-nguyen-17
  • 5,516
  • 2
  • 9
  • 20