# Modify query param in pre-request script

**URL:** <https://community.postman.com/t/modify-query-param-in-pre-request-script/8880>\
**Category:** Help Hub\
**Tags:** pre-request\
**Created:** [November 8, 2019, 3:33pm UTC](https://community.postman.com/t/modify-query-param-in-pre-request-script/8880 "2019-11-08T15:33:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![delaneyc](https://avatars.discourse-cdn.com/v4/letter/d/e9a140/32.png) [@delaneyc](https://community.postman.com/u/delaneyc)\
**Post date:** [November 8, 2019, 3:33pm UTC](https://community.postman.com/t/modify-query-param-in-pre-request-script/8880/1 "2019-11-08T15:33:31Z")

</div>

I need to programmatically set a query parameter on my request. Up until a week or two ago I was able to do the following, and it worked:

```
//url: env.com/path?param1

// update param1 to new value
let queryParam1 = 'whatever';
pm.request.url.query[0] = queryParam1;

```

This doesn’t work anymore. How do I modify (or add) a query parameter to my request in the pre-request script?

---

<div class="post-metadata">

**Author:** ![delaneyc](https://avatars.discourse-cdn.com/v4/letter/d/e9a140/32.png) [@delaneyc](https://community.postman.com/u/delaneyc)\
**Post date:** [November 8, 2019, 3:57pm UTC](https://community.postman.com/t/modify-query-param-in-pre-request-script/8880/2 "2019-11-08T15:57:27Z")

</div>

Seems I can do it like this now, but I’d still like to know if there’s a “right” way so my scripts don’t break again:

pm.request.url.query.add(‘param1=’ + paramValue);

---

<div class="post-metadata">

**Author:** ![bbrowske-LD](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@bbrowske-LD](https://community.postman.com/u/bbrowske-LD)\
**Post date:** [April 14, 2020, 10:59pm UTC](https://community.postman.com/t/modify-query-param-in-pre-request-script/8880/3 "2020-04-14T22:59:17Z")

</div>

So I’m pretty new to postman scripting, but I think this might work for you. If I know the index of my parameter to update, as it seems you are wanting to do, I have been successful with

`pm.request.url.query.idx(0).value = "newValue";`

---

<div class="post-metadata">

**Author:** ![steve.hansensmythe](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/steve.hansensmythe/32/4337_2.png) [@steve.hansensmythe](https://community.postman.com/u/steve.hansensmythe)\
**Post date:** [April 29, 2020, 7:13pm UTC](https://community.postman.com/t/modify-query-param-in-pre-request-script/8880/4 "2020-04-29T19:13:01Z")

</div>

In your pre-request script, set a variable, e.g.

postman.setEnvironmentVariable(“queryParam1”, “whatever”);

Then you can use {{queryParam1}} in your query parameters just like you set the rest of them.

---

<div class="post-metadata">

**Author:** ![mbuecher](https://avatars.discourse-cdn.com/v4/letter/m/9de0a6/32.png) [@mbuecher](https://community.postman.com/u/mbuecher)\
**Post date:** [November 25, 2021, 4:10pm UTC](https://community.postman.com/t/modify-query-param-in-pre-request-script/8880/5 "2021-11-25T16:10:47Z")

</div>

What actually worked for me all the time was:

```auto
//console.log("----- Encode query string");
//console.log("Input: " + pm.request.url.query.toString());
var querycount = pm.request.url.query.count();
for(let i = 0; i < querycount; i++) {
  //console.log(i + "/" + pm.request.url.query.idx(i).key + ": " + encodeURIComponent(pm.request.url.query.idx(i).value));
  pm.request.url.query.idx(i).value = encodeURIComponent(pm.request.url.query.idx(i).value);
}
//console.log("Output: " + pm.request.url.query.toString());

```

---

<div class="post-metadata">

**Author:** ![yweing0720](https://avatars.discourse-cdn.com/v4/letter/y/7bcc69/32.png) [@yweing0720](https://community.postman.com/u/yweing0720)\
**Post date:** [June 24, 2022, 9:37pm UTC](https://community.postman.com/t/modify-query-param-in-pre-request-script/8880/6 "2022-06-24T21:37:18Z")

</div>

to add query params there is 2 ways

```auto
pm.request.addQueryParams("auth_username={{username}}")
pm.request.url.query.add("auth_password={{password}}")

```

you can loop over `pm.request.url.query` it is an array of objects, every object has 3 properties

```auto
{
     disabled: true, // only if it is disabled 
     key: "auth_username",
     value: "{{username}}",
}

```

you can find what you are looking for and set it like you wan’t

---

<div class="post-metadata">

**Author:** ![bentis-mobimeo](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bentis-mobimeo/32/12984_2.png) [@bentis-mobimeo](https://community.postman.com/u/bentis-mobimeo)\
**Post date:** [August 21, 2022, 2:31pm UTC](https://community.postman.com/t/modify-query-param-in-pre-request-script/8880/7 "2022-08-21T14:31:41Z")

</div>

This pre-request might be useful for those wishing to modify a single named query parameter:

```auto
const { key, value } = pm.request.url.query.find(q => q.key === 'myParam')
pm.request.removeQueryParams(key)
pm.request.addQueryParams(`${key}=${doSomethingToUpdate(value)}`)

```

---

<div class="post-metadata">

**Author:** ![gnsg-swssh](https://avatars.discourse-cdn.com/v4/letter/g/50afbb/32.png) [@gnsg-swssh](https://community.postman.com/u/gnsg-swssh)\
**Post date:** [February 10, 2023, 3:46am UTC](https://community.postman.com/t/modify-query-param-in-pre-request-script/8880/8 "2023-02-10T03:46:38Z")

</div>

I have created a generic function for adding query params to request query. It will update or insert request query based on the key. It does extra check for the key is it falsey and make value empty string if value is falsey.

```auto
function upsertQueryParams(key, value) {
    if (!key) {
        throw new Error('Empty query param key.');
    }

    pm.request.url.query.upsert({ key, value: value || '' });
}

```

And the usage will be

```auto
upsertQueryParams('yourParamKey', 'paramValue');

```

---

<div class="post-metadata">

**Author:** ![davidson-dfw](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@davidson-dfw](https://community.postman.com/u/davidson-dfw)\
**Post date:** [May 29, 2024, 8:49pm UTC](https://community.postman.com/t/modify-query-param-in-pre-request-script/8880/9 "2024-05-29T20:49:04Z")

</div>

I could never get these examples to work for me. So, rightly or wrongly, I finally resorted to removing the queryParam and adding it back in the pre-request script.

```auto
// Invalid queryParam value: incoterm
const queryParam = "incoterm";

if (pm.request.url.getQueryString().includes(queryParam)) {
       pm.request.url.removeQueryParams(queryParam);
       pm.request.url.addQueryParams(queryParam+"=XXX");
};

```

---

<div class="post-metadata">

**Author:** ![vip-finastra](https://avatars.discourse-cdn.com/v4/letter/v/7bcc69/32.png) [@vip-finastra](https://community.postman.com/u/vip-finastra)\
**Post date:** [February 27, 2025, 7:45pm UTC](https://community.postman.com/t/modify-query-param-in-pre-request-script/8880/10 "2025-02-27T19:45:01Z")

</div>

FYI, for anyone who is looking at this years later and going insane thinking pm.request.url.query is an Array: it is not: [Url - Postman Documentation](https://www.postmanlabs.com/postman-collection/Url.html) shows that the url’s member “query” is a [PropertyList](https://www.postmanlabs.com/postman-collection/PropertyList.html).\<[QueryParam](https://www.postmanlabs.com/postman-collection/QueryParam.html)\>.

A look at the [source code for collection/property-list.js](https://www.postmanlabs.com/postman-collection/collection_property-list.js.html) shows that the methods on PropertyList such as filter are actually lodash.js methods.

So, act accordingly, and hopefully this saves you hours you never knew you might have lost, future keyboard warrior!
