Pre Script not pulling variable value when using Runner

I’m trying to use a runner on an api call that uses a pre-request script. The script works fine on a standalone run. The pre-request script generates a hash that the vendor validates so the value has to be correct. Below is the relevant code I’m using.

let URL_OBJECT = pm.request.url;
let QUERY = URL_OBJECT.getQueryString();

Query should end up being “name=TheClientName” when it’s pulled from the CSV.

It does do that when it actually makes the call, but the hash is being generated with “name={{Client}}”

How can I fix this?

Hey @orbital-module-expl7 :waving_hand:t3:

Welcome to the Postman Community :postman:

Would be able to share the rest of the script please and how those local variables are being used?

For it to be using or showing any type of {{...}} based syntax, I assume there must be something in the script that’s setting a variable.

Here is the whole script:

let pc = require('postman-collection');
let cryptoJS = require('crypto-js');
let moment = require('moment');

let DATE = (moment().format('ddd, DD MMM YYYY HH:mm:ss ZZ'));
pm.collectionVariables.set('duo-DATE', DATE)
console.log(DATE)
let METHOD = pm.request.method;
console.log(METHOD)
console.log (pm.request.url)
let URL_OBJECT = pm.request.url;
console.log(URL_OBJECT)

//HOST is retrieved from the collection variable itself, since parsing the hostname using
//postman-collection's URL object will just give you the variable name.
let HOST = pm.collectionVariables.get('duo-API-HOST')
console.log(HOST)
let PATH = URL_OBJECT.getPath();
let QUERY = URL_OBJECT.getQueryString();

console.log(PATH)
console.log(QUERY)

let HMAC_STRING = DATE + '\n' 
                + METHOD + '\n' 
                + HOST + '\n' 
                + PATH + '\n'
                + QUERY

console.log(HMAC_STRING)                
let HMACd = cryptoJS.HmacSHA1(HMAC_STRING, pm.collectionVariables.get('acct-skey')).toString();

pm.collectionVariables.set('duo-acct-HMAC', HMACd)

{{Client}} is a Parameter variable on the actual API Call. The script is at the folder level where this API call is.

Where is the {{client}} variable coming from?

The problem is that until the request is run, the value is not transposed. So it will just treat it as a string.

If you want the value of that variable, then you need to pull it directly from where its stored For example, directly from an Environment variable. If its in the vault then you are out of luck as you can’t retrieve those from scripts at this point in time. (It doesn’t look like you are using the vault though).

If you run the same code in the post response then you’ll find the variables will have their values.

If you console log pm.request.url from your pre-request script, that will show what you will get. You will see that it just shows the variable name as a string. Which is the problem you are experiencing and is correct at that point in time.

The variable comes from the input CSV I’m using for my runner. Can the pre-script read that in any way?

You can use the pm.iterationData() functions.

let myClient = pm.iterationData.get("Client");

Ok got it.

That does pull the right value and I can use it to manually set the query path
let QUERYNEW = QUERY.replace('{{Client}}', myClient)
but it’s not super extensible across calls.

Will have to figure out if I can do some sort of for loop for the future, but this solves the immediate issue.

Thanks!