I’ve got a use case where I have an API URL I want to call with a different parameter per environment (dev/stage/prod). I’d like to use a variable within another variable, i.e. I have a global url variable set to http://localhost:8080/{{short_tier}} and then I’d like to set a per-environment variable called short_tier that should be getting interpolated into the global variable, but that doesn’t seem to resolve (it stays as http://localhost:8080/{{short_tier}}). Is this possible, or am I going about this all wrong?
I’m currently doing this as a workaround:
var laps_url = pm.variables.get('laps_url');
var short_tier = pm.variables.get('short_tier');
// Variables do not resolve nested variable values, so we need to do that manually
laps_url = laps_url.replace('{{short_tier}}', short_tier);
but that seems a little cumbersome. Again, I’m probably not thinking about this the right way, so please let me know if there is a more “Postman-y” way to accomplish this. ;]
When I call pm.variables.get('laps_url'); in the Pre-request Script for the collection, it resolves to http://localhost:13449/token/{{short_tier}}, i.e. the {{short_token}} variable is not replaced.
@carsten.dreesbach.op pm.variables.get will only return you value for that particular variable. But let’s say in your request URL if you use {{laps_url}}, it will resolve to http://localhost:13449/token/dev
Ah, got it - so this is for a request to another API before making the actual API call, in which case I’m guessing what I’m doing with the manual variable substitution is what I’m going to need to do, right? Or is there another alternative?
Also, I can confirm that if I use it in the request URL, it resolves correctly. It’s just that I want to use it in a pre-request script instead.
Strange, this used to work with the Postman app extension! I just updated from the Chrome extension to this and now it doesn’t work.
I have:
host = localhost
port = 8080
url = http://{{host}}:{{port}}
interesting is this error in the console, where is that %0A (newline) coming from?
Update: I thought it might be because of importing the settings and maybe that is the problem. If I manually recreated the 3 setting variables, host, port, and url it then worked. I suspect that the import was inserting an extra %0A (newline) character.
Hi all. Sorry for responding to an old post, but the issue I am having with variables that contain variables is not in the environment or global definition, but in a call to the variables in a pre-request or test script. I am using pm.variables.get(“OUTTER_VARIABLE_NAME”) and it does get that value, but it does not expand the INNER variable so I end up with {{INNER_VARIABLE}}/x/y/z.
Note: Variables within Variables does not work in the UI. For example if you create a Collection variable foo referencing {{someEnvVariable}} it will not work even if the env variable someEnvVariable exists. This is unfortunate
It depends on how you’re trying to resolve that string variable, everything that’s saved to a variable is a string.
In a script, you would need to use pm.variables.replaceIn('{{var_name}}') to resolve that string value so that the variable inside the string is correctly used.