Is there a way to ensure Data Variables are assigned prior to the Pre-Request Script executing?

Hi there,

I have a Pre-Request Script that encrypts the whole request data and then stores it in a Global Variable called signature, which is sent as a Header in the request.

var hash = CryptoJS.HmacSHA256(request.data, environment.API_Secret);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
postman.setGlobalVariable("signature", hashInBase64);

I’m running a Collection that replaces the values in the Body of my requests from a CSV data file.

My expectation is that the variables in the Body of the request would be replaced with those from the CSV file and then the Pre-Request Script would run considering those values. and in my example, it would generate unique signatures each time.

Instead, what ends up happening is the the Pre-Request Script uses the variables themselves - e.g. {{username}} from the Body rather than the assigned values - e.g. “foo” from the CSV and results in the same signature for each request.

Is there a way to ensure the Pre-Request Script runs after the Data variables have been replaced with those from the CSV?

Many thanks! Yordan

In the pre-request script, does it work if you try pm.iterationData.get(“key”) instead of request.data?

The execution steps when postman runs a request:

  1. Pre-request script
  2. Request (This is where the variables will get resolved in the request body)
  3. Test Script

The pre-request script runs right before the request is executed.

As @jetison mentioned above, you can directly access the data variables for each request in the pre-request script itself - this is just what you need.

You can use the pm.iterationData.get('variable_key') in your pre-request script.
Then you can build your hashInBase64 using those values (it’ll be a bit more work than directly accessing request.data - since you’ll have to build this data manually using the script) and finally set them as a global variable.

If you’re still having doubts around this implementation, let me know and I’ll be happy to share a detailed example with you.

@singhsivcan @jetison Thanks a lot for your contribution - an example would be very much appreciated. I have around a hundred data variables that I am pulling from the CSV to build my requests.

@iordo - here’s an example tutorial that shows how to use data variables in scripts and also how to properly format the CSV data file: Looping through a data file in the Postman Collection Runner.

@jetison @singhsivcan Thanks but could I ask you for an example? If my request.data looks like below, how would I build the same using pm.iterationData.get?

{
  "payload": {
    "control": [
        {
                "option": "OPTION_1",
                "value": "{{Data_CSV_Variable_1}}"
            },
            {
                "option": "OPTION_2",
                "value": "{{Data_CSV_Variable_2}}"
            },
            {
                "option": "OPTION_3",
                "value": "{{Data_CSV_Variable_3}}"
            }
    ]}
}