My question:
Is there any way to switch between a group of variables with same name?
For example, I have two variables: latitude, longitude,
values 1: latitude=1.1, longitude=1.2
values 2: latitude=2.1, longitude=2.2
I want to quickly switch between these two values.
I’ve already tried:
I tried set them in different environment, but what if I have many groups like
group 1, two variables: latitude, longitude
group 2, three variables: anotherVariable1, anotherVariable2, anotherVariable3
It seems I cannot apply both two environments so it’s impossible.
For now I just manually change the variable to something like latitude_CA, latitude_NY, and change the variable name in my request page. It’s very inconvenient.
Environment’s would sound like the way to go. One for NY and one for CA.
However, you’ve said that you might have different numbers of variables, and I can’t see how this would be possible as I suspect that would make the request fundamentally different.
The requests surely need to be the same format with the same number of elements for any of this to work.
I don’t know how you are aiming the run the requests. (Collection Runner, Newman, CLI, etc).
You might want to consider duplicating the request and separating them by folders which can be run separately using command line options (for example in Newman).
What is the trigger for when values1 need to be used, or when values2 need to be run (or for when the request that uses more variables).
If there isn’t a trigger, then the only way would be by manually changing the variables or values.
I’m using collection runner. I’m an client developer not an API developer, so I just click send button on collection runner page. What I need is more easy way to switch between a number of variable groups with different values.
This can be achieved using Environments but only if the requests are fundamentally the same, with the same elements.
It won’t work if some of the Environments have more elements\variables, as this means you would also need to update the body to match.
Technically speaking you could store the whole body (JSON) as an Environment variable (stored as an string) and use a pre-request script to set this as the body for the request.
const body = JSON.parse(pm.collectionVariable.get("body")); // retrieve Environment variable
pm.request.body.raw = body; // set body for current request
// console.log(pm.request.body.raw);