# Evaluate Embedded Variables "All the Way Down"

**URL:** <https://community.postman.com/t/evaluate-embedded-variables-all-the-way-down/2228>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [August 2, 2018, 5:20pm UTC](https://community.postman.com/t/evaluate-embedded-variables-all-the-way-down/2228 "2018-08-02T17:20:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave\_Westerman](https://avatars.discourse-cdn.com/v4/letter/d/e19adc/32.png) [@Dave\_Westerman](https://community.postman.com/u/Dave_Westerman)\
**Post date:** [August 2, 2018, 5:20pm UTC](https://community.postman.com/t/evaluate-embedded-variables-all-the-way-down/2228/1 "2018-08-02T17:20:47Z")

</div>

We make heavy use of environment variables in Postman. But I’ve run into two roadblocks:

1. `pm.environment.get()` (and its equivalents) do not evaluate embedded variables. So if I get a variable that has another variable in it, `{{var1}}` is not replaced by `val1`.

I got around this problem by writing a common function that I call in all my tests instead of `pm.environment.get()`. The function is called recursively until all the variables aree evaluated:

```auto
var funcGetAndEvalEnvVar = (varName) => {
  const EMBEDDED_VAR_REGEX = new RegExp(`{{\\w+}}`, 'g');
  var varValue = environment[varName];
  var evalVarValue = varValue;
  var reMatch;
  while (reMatch = EMBEDDED_VAR_REGEX.exec(varValue)) {
    var embeddedVar = reMatch[0];
    var embeddedVarName = embeddedVar.slice(2, -2);
    if (environment.hasOwnProperty(embeddedVarName)) {
      var embeddedVarValue = funcGetAndEvalEnvVar(embeddedVarName);
      evalVarValue = evalVarValue.replace(new RegExp(embeddedVar, 'g'), embeddedVarValue);
    }
  }
  return evalVarValue;
};

```

1. After resolving the issues with #1 above, I thought I was in good shape. However, I also use variables in the request bodies, that contain embedded variables at multiple levels. The variables seem to only be evaluated at a limited number of levels. So I’m still having the same problems.

I tried to write the following pre-request script, but of course, according to the doc, the `request` object is read-only (I created this script just in case it might be wrong!):

```auto
var REQUEST_DATA_VAR = 'requestData';
pm.environment.set(REQUEST_DATA_VAR, request.data);
request.data = funcGetAndEvalEnvVar(REQUEST_DATA_VAR);

```

My feature request is that it would be very helpful if all environment variables at all levels would be evaluated by Postman.

If this is not possible / feasible, then my alternate feature request would be to give the pre-request script a way to update the data in the request, instead of it being read-only.

---

<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:** [December 9, 2024, 2:34pm UTC](https://community.postman.com/t/evaluate-embedded-variables-all-the-way-down/2228/2 "2024-12-09T14:34:44Z")

</div>

Closed on December 9, 2024, 2:34pm UTC

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