# How do I get the real data of environment variables in pre-req?

**URL:** <https://community.postman.com/t/how-do-i-get-the-real-data-of-environment-variables-in-pre-req/62763>\
**Category:** Help Hub\
**Tags:** variables, environments, md5\
**Created:** [June 28, 2024, 8:11am UTC](https://community.postman.com/t/how-do-i-get-the-real-data-of-environment-variables-in-pre-req/62763 "2024-06-28T08:11:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![GreenKing19](https://avatars.discourse-cdn.com/v4/letter/g/e56c9b/32.png) [@GreenKing19](https://community.postman.com/u/GreenKing19)\
**Post date:** [June 28, 2024, 8:11am UTC](https://community.postman.com/t/how-do-i-get-the-real-data-of-environment-variables-in-pre-req/62763/1 "2024-06-28T08:11:29Z")

</div>

I have a pre-req script that does md5 encryption and then passes it back to the server, and when the data is a variable, what is encrypted is the variable name, not the real data of the variable. How to deal with it?

Here’s an example of a request👇

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/f/f/ff376b84b15eda374794a860a31bf58daae9837b.png)

variable

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/d/a/dae54419a554a3942e782a202d2090f5a9d9ebef.png)

pre-req script

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/b/abe632c22b0465fc76a935224d3f3be682776d7d.png)

variable `testmd5` = “hello!world”

Expected 👨🏻‍💻

```javascript
const testMd5 =MD5("hello!world")
// output: "4B460DD7310C9F783242F2809CDFC2EE"

```

Actually 🫤

```javascript
const testMd5 =MD5("{{testmd5}}")
//output: "f5fa842484de91794470c780b167183c"

```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [June 28, 2024, 8:58am UTC](https://community.postman.com/t/how-do-i-get-the-real-data-of-environment-variables-in-pre-req/62763/2 "2024-06-28T08:58:55Z")

</div>

Hey @GreenKing19 👋🏼

Welcome to the Postman Community :postman:

As you’re extracting that raw string from the request body, it’s just using the string value of the variable before it’s been resolved.

You’d need to use something like:

```auto
const MD5 = require('crypto-js').MD5;

const requestData = JSON.parse(pm.request.body.raw)
const testmd5 = requestData.testmd5
const md5String = MD5(pm.variables.replaceIn(testmd5)).toString()

console.log(md5String);

```

How are you setting that newly converted string in the request body?

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [June 28, 2024, 10:26am UTC](https://community.postman.com/t/how-do-i-get-the-real-data-of-environment-variables-in-pre-req/62763/3 "2024-06-28T10:26:53Z")

</div>

Pretty much what Danny has said.

```auto
const MD5 = require('crypto-js').MD5
let myVar = pm.variables.get("testmd5"); // or pm.environment.get
const md5String = MD5(myVar).toString();
console.log(md5String);

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/1/61a60642b28b1f2f8ad22d894d6d06246a5ee740.png)

What do you mean by “pass it back to the server”. Do you need to update the value of the environment variable with the encrypted string? (Which is easy enough, just add pm.environment.set to the end of the script).

---

<div class="post-metadata">

**Author:** ![GreenKing19](https://avatars.discourse-cdn.com/v4/letter/g/e56c9b/32.png) [@GreenKing19](https://community.postman.com/u/GreenKing19)\
**Post date:** [July 1, 2024, 5:58am UTC](https://community.postman.com/t/how-do-i-get-the-real-data-of-environment-variables-in-pre-req/62763/4 "2024-07-01T05:58:49Z")

</div>

I know this way, but sometimes I don’t know what the variable name is, because my pre-req is written inside the collection.  
Sometimes it’s a variable, sometimes it’s an environment variable  
😖

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [July 1, 2024, 9:28am UTC](https://community.postman.com/t/how-do-i-get-the-real-data-of-environment-variables-in-pre-req/62763/5 "2024-07-01T09:28:35Z")

</div>

> [@GreenKing19](#):
>
> Sometimes it’s a variable, sometimes it’s an environment variable

Is it at least the same name each time?

If you use pm.variables, it will get the variable with the narrowest scope.

```auto
let myVar = pm.variables.get("testmd5"); // or pm.environment.get

```

[Postman Variable Scopes | Postman Learning Center](https://learning.postman.com/docs/sending-requests/variables/variables/#variable-scopes)

So it will be this order.

Local → Data → Environment → Collection → Global - Postman

If you only have the variable set as an environment variable, then it will be fine. If you have the variable with the same name set in multiple locations, then it will read the narrowest value. So a local variable will take precedence over an environment variable if they have the same name.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [July 31, 2024, 9:29am UTC](https://community.postman.com/t/how-do-i-get-the-real-data-of-environment-variables-in-pre-req/62763/6 "2024-07-31T09:29:18Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
