Modify query param in pre-request script

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

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.