Issue with Environment Variable Resolution

I am currently experiencing an issue with environment variable resolution within a pre-request script in Postman. Here are the details of the problem:

Description: I have set up an environment variable named organization_url, with an initial value of {{base_url}}/organizations. When I use this variable in a request URL, the value resolves correctly. However, when I attempt to retrieve the value of organization_url within a pre-request script using pm.variables.get("organization_url"), it does not return the resolved value; instead, it returns “{{base_url}}/organizations.”

Steps I’ve Taken:

  1. Ensured that the variable name in the script matches the name in the environment (organization_url).
  2. Confirmed that I am running the request within the correct environment.
  3. Verified that the environment variable’s value is correctly set to {{base_url}}/organizations.
  4. Attempted to use a different variable name in the script (test_variable) with the same value to isolate the issue, and encountered the same problem.

Expected Behavior: I expect the pre-request script to retrieve the resolved value of organization_url, which should be

Actual Behavior: The pre-request script currently returns “{{base_url}}/organizations” instead of the resolved value.

I would greatly appreciate your assistance in resolving this issue. Please let me know if you need any further information or if there are any troubleshooting steps I should follow.

Hey @hooper7775 :wave:

Welcome to the Postman Community! :postman:

To use the variable like that in a script, you’re going to need to use pm.environment.replaceIn('{{organization_url}}') so that it can resolve the variable, that you have within that string.

1 Like

Wow, thanks, you are the best i was trying with pm.environment.replaceIn({{organization_url}}), but it didn’t work right cause i haven’t used “”. Why in documentation it is written so strange? ```

You’re welcome. :trophy:

That page only shows which datatype is required in the function. A better page with an example, would have maybe been this one:

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