Is it possible to update an environment variable within a collection pre-request script and have access to that updated value in the actual request?
Assume you have a simple GET request which uses an variable within the URL, e.g:
http://example.com/api/thing/{{ID}}
Where {{ID}}
is stored as an environment variable.
Sometimes, this data needs to be modified before the request runs. You can use a pre-request script to update the value, however, as the request has already been initiated, the updated value is not used in the URL above; instead, the initial (unmodified) value is used.
Is it possible to use it somehow to force a refresh of the variables used should this occur?
Example:
If the environment variable ID
is set to âfoo123456â, then:
Example, pre-request script:
const ID = pm.environment.get("ID");
if (ID.startsWith("foo")) {
pm.environment.set('ID', "bar" + ID);
};
This does update the Environment variable, however, the request is incorrect:
Desired result:
http://example.com/api/thing/barfoo123456
Actual result:
http://example.com/api/thing/foo123456
Of course, running the request a second time works as expected.