Generate Random/Dynamic data in Postman Requests

How To Generate Random Dynamic Data In Requests

In the previous articles on Postman Tutorial, we have covered “API Test Cases in Postman using JavaScript

In this “Generate Random/Dynamic Data in Requests” article, I will be demonstrating as to how you can implement this concept and get a tight grip over this.

We can generate random/dynamic data in requests using the following functions.

  1. Math.random()
  2. $randomInt
  3. JavaScript Lodash _.
  4. JavaScript Moment

Postman Dynamic Data

Method 1: Using Math.random() function in Pre-request Scripts

Suppose we want to generate a random number for customerId field every time an API is hit. Random function in the Math library is used to generate random numbers between 1 and 100. Then variable is set as a global variable and later on the global variable is used in the body to generate random data.

Under Pre-request Scripts tab:

Postman Math Random Prerequisities

Under Body tab:

Postman Math Random Body

Method 2: Using Postman built-in variables to generate Random data

Suppose we want to generate a random value for the quantity field. We can use Postman built-in variable randomInt to generate random integer value.

Under Body tab:

Postman Built In Random Variables Body


Other commonly used built-in variables are:

Postman Built-in Variables

Method 3: Using JavaScript Lodash module (_.) to generate Random data

We have some arrays of common first names and last names. Then we will be randomly picking things from those arrays and setting them in the Postman variables objects. The Postman variables are available to the collection while the tests are running, but then they go away so they’re not cluttering everything up while the tests aren’t running. Random functionality comes from a JavaScript library called Lodash _. which is available automatically.

Under Pre-request Scripts:

Postman JavaScript Lodash

Under Body tab:

Postman JavaScript Lodash Body

Method 4: Using Moment module in JavaScript for date formatting

Moment is a JavaScript library that parses, validates, manipulates, and displays dates and times in JavaScript. It makes it a lot easier than just the native JavaScript date. In order to input it and bring it into our script, we just need to do require() method. Once we have this require() method with moment, we can call this moment object to do all of our date manipulation.

Under Pre-request Scripts:

Postman Moment Module

Under Body tab:

Postman Moment Module Body

Next steps:

Learn “Extracting data from responses and Chaining requests” in the next tutorial.

Author Bio: This article is written by Harsha Mittal an ISTQB-CTFL Certified Software Test Engineer having 3.5+ years of experience in Software Testing.

Related posts:

Sharing is caring.

Share on facebook
Share on twitter
Share on linkedin

Like This Post?

We have a lot more where that came from?

We only send really good stuff occasionally, promise.

Rajkumar SM

Leave a Comment

Your email address will not be published. Required fields are marked *

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on whatsapp

Recent Posts:

Scroll to Top
API Testing eBook