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

**URL:** <https://community.postman.com/t/is-there-a-way-to-ensure-data-variables-are-assigned-prior-to-the-pre-request-script-executing/3599>\
**Category:** Help Hub\
**Tags:** variables, scripts, csvdatafile\
**Created:** [December 14, 2018, 5:50am UTC](https://community.postman.com/t/is-there-a-way-to-ensure-data-variables-are-assigned-prior-to-the-pre-request-script-executing/3599 "2018-12-14T05:50:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iordo](https://avatars.discourse-cdn.com/v4/letter/i/e36b37/32.png) [@iordo](https://community.postman.com/u/iordo)\
**Post date:** [December 14, 2018, 5:50am UTC](https://community.postman.com/t/is-there-a-way-to-ensure-data-variables-are-assigned-prior-to-the-pre-request-script-executing/3599/1 "2018-12-14T05:50:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jetison](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jetison/32/12_2.png) [@jetison](https://community.postman.com/u/jetison)\
**Post date:** [December 14, 2018, 6:21am UTC](https://community.postman.com/t/is-there-a-way-to-ensure-data-variables-are-assigned-prior-to-the-pre-request-script-executing/3599/2 "2018-12-14T06:21:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [December 14, 2018, 6:26am UTC](https://community.postman.com/t/is-there-a-way-to-ensure-data-variables-are-assigned-prior-to-the-pre-request-script-executing/3599/3 "2018-12-14T06:26:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![iordo](https://avatars.discourse-cdn.com/v4/letter/i/e36b37/32.png) [@iordo](https://community.postman.com/u/iordo)\
**Post date:** [December 14, 2018, 1:32pm UTC](https://community.postman.com/t/is-there-a-way-to-ensure-data-variables-are-assigned-prior-to-the-pre-request-script-executing/3599/4 "2018-12-14T13:32:39Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![jetison](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jetison/32/12_2.png) [@jetison](https://community.postman.com/u/jetison)\
**Post date:** [December 14, 2018, 7:32pm UTC](https://community.postman.com/t/is-there-a-way-to-ensure-data-variables-are-assigned-prior-to-the-pre-request-script-executing/3599/5 "2018-12-14T19:32:34Z")

</div>

@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](http://blog.getpostman.com/2018/04/11/looping-through-a-data-file-in-the-postman-collection-runner/).

---

<div class="post-metadata">

**Author:** ![iordo](https://avatars.discourse-cdn.com/v4/letter/i/e36b37/32.png) [@iordo](https://community.postman.com/u/iordo)\
**Post date:** [December 16, 2018, 11:36pm UTC](https://community.postman.com/t/is-there-a-way-to-ensure-data-variables-are-assigned-prior-to-the-pre-request-script-executing/3599/6 "2018-12-16T23:36:42Z")

</div>

@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}}"
            }
    ]}
}
```
