Ask Your Question
3

What is the method of storing a list of values in Postman and selecting a random value from the list to use in an API call?

asked 2021-06-25 11:00:00 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-01-09 15:00:00 +0000

lakamha gravatar image

The method of storing a list of values in Postman is by creating a CSV file with the list of values and then uploading it in the Data section of the Postman collection.

To select a random value from the list to use in an API call, follow these steps:

  1. In the request where the value needs to be inserted, use double curly braces {{}} to surround the placeholder where you want the random value to be inserted. For example, "{{randomValue}}".

  2. In the Pre-request Script section of the request, write the following code:

const data = pm.iterationData.toObject();
const randomValue = data[Math.floor(Math.random() * data.length)];
pm.environment.set("randomValue", randomValue);

This code retrieves the list of values from the CSV file, selects a random value from the list, and sets it as an environment variable named "randomValue".

  1. Save the Pre-request Script and run the request. The selected random value will be inserted in the request where the placeholder was added.
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-06-25 11:00:00 +0000

Seen: 7 times

Last updated: Jan 09 '23