# Change body between predefined values

**URL:** <https://community.postman.com/t/change-body-between-predefined-values/40601>\
**Category:** Help Hub\
**Tags:** scripts, pre-request, request\
**Created:** [October 30, 2022, 12:23am UTC](https://community.postman.com/t/change-body-between-predefined-values/40601 "2022-10-30T00:23:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaga85](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@shaga85](https://community.postman.com/u/shaga85)\
**Post date:** [October 30, 2022, 12:23am UTC](https://community.postman.com/t/change-body-between-predefined-values/40601/1 "2022-10-30T00:23:43Z")

</div>

Hello there!

Is there a way to change a body between predefined values everytime i run a POST request?

For example:

```auto
{
    "id": 11,
    "name": "Tutorialspoint",
    "username": "Test1",
    "email": "abc@gmail.com",
}

```

i want the username to change between ´Test1, Test2, Test3ˋ everytime i run the request.  
Is it possible?

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [October 30, 2022, 9:57am UTC](https://community.postman.com/t/change-body-between-predefined-values/40601/2 "2022-10-30T09:57:39Z")

</div>

Hi @shaga85

You’re looking for ‘data driven testing’.  
Here are some resources:

[https://learning.postman.com/docs/running-collections/working-with-data-files/](https://learning.postman.com/docs/running-collections/working-with-data-files/)

> [@API Testing 'A Beginners View': Data-driven testing in Postman](https://community.postman.com/t/api-testing-a-beginners-view-data-driven-testing-in-postman/20302):
>
> Hi Everyone!!! Let’s check out some details about Data-Driven Testing with Postman using CSV and JSON files😊 Data-driven testing can be a very effective approach in testing an API against different data-sets. Postman supports CSV and JSON files to get data for the test scripts. The data-driven approach is useful when we need to execute a test with multiple sets of Data. Also, modifying or adding any data fields will just need updating the data files which is easier than going to test script and…

[![](https://img.youtube.com/vi/fr7UpFNQbLw/maxresdefault.jpg "Data-driven testing using Postman") ](https://www.youtube.com/watch?v=fr7UpFNQbLw)

---

<div class="post-metadata">

**Author:** ![shaga85](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@shaga85](https://community.postman.com/u/shaga85)\
**Post date:** [October 30, 2022, 10:21am UTC](https://community.postman.com/t/change-body-between-predefined-values/40601/3 "2022-10-30T10:21:35Z")

</div>

Nice, this will come in handy! Altough its not exactly what i need. I only want to have the request with one of the usernames (Test1 or Test2 or Test3), and not run alll 3 names like on the collection runner. Also i would like to only run this test without the need of the collection runner.  
Is this possible?

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [October 30, 2022, 10:46am UTC](https://community.postman.com/t/change-body-between-predefined-values/40601/4 "2022-10-30T10:46:48Z")

</div>

If its only 1 request you could set it as a variable and pass it in using the {{variable}} syntax.

You could add a pre-req script to set the variable before the request is sent.

How do you decide which user you want to send?

---

<div class="post-metadata">

**Author:** ![shaga85](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@shaga85](https://community.postman.com/u/shaga85)\
**Post date:** [October 30, 2022, 11:04am UTC](https://community.postman.com/t/change-body-between-predefined-values/40601/5 "2022-10-30T11:04:00Z")

</div>

It can be random between those 3 usernames (Test1, Test2, Test3). I don’t need to have a specific order. How do I do that on the pre-request test?

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [October 30, 2022, 10:04pm UTC](https://community.postman.com/t/change-body-between-predefined-values/40601/6 "2022-10-30T22:04:45Z")

</div>

You could do something like this;

```auto
//Create an array with all options
let myList = ["User 1", "User 2", "User 3"];
//Generate a random number between 1 and array length
let i = Math.floor(Math.random() * myList.length);
//Output the value by using the random number as the array index
console.log("Output variable = " + myList[i]);

```

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [November 1, 2022, 2:26pm UTC](https://community.postman.com/t/change-body-between-predefined-values/40601/7 "2022-11-01T14:26:19Z")

</div>

The Lodash library is built-in to the Postman sandbox by default so you can use \_.sample.

```auto
var randomString = _.sample(['Test1', 'Test2', 'Test3']);
console.log(randomString);

```

> **[Postman JavaScript reference | Postman Learning Center](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#using-external-libraries)**
>
> Postman JavaScript reference: documentation for Postman, the collaboration platform for API development. Create better APIs—faster.

> **[Lodash](https://lodash.com)**
>
> A JavaScript utility library delivering consistency, modularity, performance, & extras.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [September 1, 2023, 11:09am UTC](https://community.postman.com/t/change-body-between-predefined-values/40601/8 "2023-09-01T11:09:07Z")

</div>

Closed on September 1, 2023, 11:09am UTC


