Variables within variables?

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!

1 Like

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

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.

@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.

@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 . We will keep you updated in case of any new development towards this feature.

1 Like

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?

GET http://localhost/ :8080/Patient?family=Jones

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.