# Randomize query params

**URL:** <https://community.postman.com/t/randomize-query-params/40632>\
**Category:** Help Hub\
**Tags:** scripts, request, data\
**Created:** [October 31, 2022, 1:48pm UTC](https://community.postman.com/t/randomize-query-params/40632 "2022-10-31T13:48:58Z")\
**Posts on this page:** 3\
**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 31, 2022, 1:48pm UTC](https://community.postman.com/t/randomize-query-params/40632/1 "2022-10-31T13:48:58Z")

</div>

Hi!

Im trying to randomize params for a query request by using pre-request Script. At the moment i have:

```auto
let queryParams = '';

const nft = {
    owneraddress: "0x434235346353f010C052440C1486",
};
queryParams = '?' + Object.keys(nft).map(key => `${key}=${nft[key]}`).join('&');
pm.globals.set("queryParams", queryParams);

const nft1 = {
    active: "true",
};
queryParams = '?' + Object.keys(nft1).map(key => `${key}=${nft1[key]}`).join('&');
pm.globals.set("queryParams", queryParams);

```

 ![Screenshot 2022-10-31 at 13.38.59](https://us1.discourse-cdn.com/getpostman/original/3X/9/d/9d9c139bf0579491cc936e84f756f34b9603fefc.png)

Is there a was to randomize what params to add to the query request? Lets say that i run it now, so the nft params are added, then if i run it again the nft1 is added, or even combine both params.

Sounds like a tricky one, and i dont even know if it possible.

Any ideias?

---

<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 31, 2022, 3:39pm UTC](https://community.postman.com/t/randomize-query-params/40632/2 "2022-10-31T15:39:42Z")

</div>

So i think i figured out a solution.

Here it is, might help someone:

```auto
let queryParams = '';

const nft = {
    owneraddress: "0x486AB254e9574534B3d298C4FC3f0234234230C1486",
    name: "nftMint",
    active: "true",
    servicename: "trybegame",
};

let headers = {};

const numberOfArguments = Math.floor(Math.random() * Object.keys(nft).length - 1) + 2;

var nftKeysList = Object.keys(nft);
var randomKeys = [];
do {
  randomKeys[randomKeys.length] = nftKeysList.splice(Math.floor(Math.random() * nftKeysList.length), 1)[0];
} while (randomKeys.length < numberOfArguments + 1);

for (let i = 0; i < randomKeys.length - 1; i ++) {
  headers[randomKeys[i]] = nft[randomKeys[i]]
}
console.log(numberOfArguments);

queryParams = '?' + Object.keys(headers).map(key => `${key}=${headers[key]}`).join('&');
pm.globals.set("queryParams", queryParams);

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [October 2, 2023, 6:18pm UTC](https://community.postman.com/t/randomize-query-params/40632/3 "2023-10-02T18:18:07Z")

</div>

Closed on October 2, 2023, 6:18pm UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
