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!

3 Likes

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

1 Like

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 https://github.com/postmanlabs/postman-app-support/issues/5767 . 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.

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.

Advice on how to get this to work?

Thanks!

Kevin

Disregard the post above. I figured out that we need to use pm.variable.replaceIn instead of pm.variable.get

pm.variables.replaceIn("{{OUTTER_VARIABLE_NAME}}") will return to you the contents of the variable and expand any inner variables that may be defined.

2 Likes