Variables within variables?


#1

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. ;]

Thanks in advance for any input you might have!


#2

@carsten.dreesbach.op Postman does support variables inside a variable. Is it possible for you to add some screenshot


#3

Sure thing @prashant_postman - hope this illustrates it.

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.


#4

@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


#5

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.


#6

@carsten.dreesbach.op Your use case makes a lot of sense. We will definitely look into making this better. Also we are already tracking this feature request here https://github.com/postmanlabs/postman-app-support/issues/5767 . We will keep you updated in case of any new development towards this feature.