Query variable from within Pre-request script

Good day,

I am writing a collection-level script that will dynamically generate a signature based on all the query params. The issue is that if I use a dynamic variable in the request, it is not getting replaced with the current value. For example, the request is defined like so:

image

where companyId is saved in the login request. I then access via the pre-request script like so:

    pm.request.url.query.all().forEach((param) => {
        console.log(param);
    });

and it comes back like so:

{key: “p_com_id”, value: “{{companyId}}”}

But I need that value to come back replaced with the current value. Any way to do this?

Note that I cannot hard-code or get via pm.environment because each call may use a slight variation on the key name. Perhaps I could check for {{xxx}} and do a lookup that way - any examples of this?

Thank you,
Tyler

Okay, to answer my own question: I initially wrote a method that looked for the {{}} in the name, and then call the pm.variables.get.

But then I discovered the pm.variables.replaceIn() method, which will look up the value for me, or simply return back the passed in value. This is exactly what I needed.

Thank you.

1 Like