delaneyc
(Chris Delaney)
November 8, 2019, 3:33pm
1
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?
1 Like
delaneyc
(Chris Delaney)
November 8, 2019, 3:57pm
2
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);
2 Likes
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";
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.
mbuecher
(mbuecher)
November 25, 2021, 4:10pm
5
What actually worked for me all the time was:
//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());
2 Likes
to add query params there is 2 ways
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
{
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
This pre-request might be useful for those wishing to modify a single named query parameter:
const { key, value } = pm.request.url.query.find(q => q.key === 'myParam')
pm.request.removeQueryParams(key)
pm.request.addQueryParams(`${key}=${doSomethingToUpdate(value)}`)
1 Like
gnsg-swssh
(Seah Sheng Hong)
February 10, 2023, 3:46am
8
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.
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
upsertQueryParams('yourParamKey', 'paramValue');
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.
// 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");
};