My question: I’m trying to set up a monitor for a couple endpoints. I apologize in advance for my knowledge level here. I’m not even completely sure how to describe the problem, so searching for existing answers came up flat.
Here goes:
I want to test an endpoint with a fixed base URL with one variable. The variable will have a list of values that I want tested. Think of the variables as tokens pulling different data from the same endpoint which represents brands (Coke, Pepsi, RC Cola, 7Up…whatever)
Like this - {{base URL}}?api_token={{variable}}
I’d like to run the test every couple hours, pulling a random value (from the list) for the variable each time. Is that possible? How would someone do this. Keep in mind that I can read and follow concepts, but I don’t consider myself experienced working with APIs.
Or, is the easiest thing just to set up a fixed URL for every version of the variable and just run them all, all the time?
Where and how will you be storing the variables? Could you provide an example, please?
For example, if you have them at the Collection level as an array [1,2,3,4,5], you could do this to randomly select an item from the list on each monitor run:
let ids = JSON.parse(pm.collectionVariables.get('ids'));
pm.variables.set('variable', `${_.sample(ids)}`);
Your solution seems simple enough that maybe I can even stumble my way through it. I’m not exactly sure how to store the array (which is actually just a list of 64-character API Keys) in the collection. So I googled that bit, and found this link which is definitely getting at what I’m trying to do. It’s good to understand the name of this outcome is to ‘loop through’ the array. I guess I should have realized that.
The example at the link, though, doesn’t seem to be randomizing the swapped-in attribute. He’s calling for it specifically each time, so I have to look closely at what you’re doing here, and what this guy is doing to see if I can figure that out.
It looks to me like he’s storing his array right in the pre-request script (another key concept I learned today). I assume I could do the same in your example?
You’re saying that I can store these 64-character keys as variables in the collection, and then pull one variable each run using your script. Okay. That’s great.
When I add the variables to the collection, is each key a new variable, or is there just one variable that I somehow assign an array of possible values to?
I see that when I create a variable, I have an Initial Value choice and a Current Value choice. It seems like this piece of help is telling me that I can store all the options of the array in one variable (which you’re calling ‘ids’) if I first stringify it. I feel like that’s getting warmer?