koustubh24
(Koustubh.K)
October 14, 2024, 3:02am
1
Bit of newbie here , so need some help from Postman experts out there ,
Trying to achieve : as part of my tear down, I’m trying to set “NULL” / BLANK for all the existing collection variables.
Problem : tried 2 logics but both of them DO NOT set any of the collection variables to NULL / BLANK.
Logic#1 :
// getting all collection variables
const allCollVariables = pm.collectionVariables.values.map(function(variable) {return variable.key; });
// filtering out base_url variable
const excludeBaseURL = allCollVariables.filter(function(variable) {
return !variable.toLowerCase().includes("base_url"); });
// clearing all the remaining variables
return excludeBaseURL.forEach(function(variableName) {
pm.collectionVariables.set(variableName, null); });
Logic#2 :
// getting all collection variables
const allCollVariables = pm.collectionVariables.values.map(function(variable) {return variable.key; });
// filtering out base_url variable
const excludeBaseURL = allCollVariables.filter(function(variable) {
return !variable.toLowerCase().includes("base_url"); });
// clearing all the remaining variables
for (var i = 0; i < excludeBaseURL.length; i++) {
pm.collectionVariables.set(excludeBaseURL[i], null);
}
Note : I tried using " " instead of null, but it didn’t work…
Can someone please help me out here !
Hey @koustubh24
Welcome to the Postman Community!
Could you something like this to set them to a blank value?
const resetCollectionVars = (prefix) => {
const variableKeys = Object.keys(pm.collectionVariables.toObject());
variableKeys.forEach((arrItem) => {
if (!arrItem.startsWith(`${prefix}`)) {
pm.collectionVariables.set(arrItem, "");
}
});
}
resetCollectionVars("base_url");
koustubh24
(Koustubh.K)
October 14, 2024, 8:00pm
3
thank you @danny-dainton for that… I copied that in the Collection > Post-response script and tried referencing in one of the request’s Post-response script but it didn’t work … is there a particular way to reference it?
There are a few ways but I would recommend using the Package Library to add the script.
1 Like
koustubh24
(Koustubh.K)
October 14, 2024, 8:11pm
6
Unfortunately, can’t see the “Package” option in my Postman (v11.16.1).
Is that not here?
Do you have this in a Public Workspace?
koustubh24
(Koustubh.K)
October 14, 2024, 8:22pm
8
I’m using a “shared-workspace” with just 1 other tester & this is what I see. …
I can’t see the Workspace visibility from that image.
The package library was added for v11 so it would be part of your version.
The only way it wouldn’t be showing is if you’re working in a Public Workspace.
koustubh24
(Koustubh.K)
October 14, 2024, 9:04pm
10
sorry about that, but here is a “private” workspace of mine & I still can’t see the package options …
Collection:
Workspace settings:
1 Like
That’s strange, if you’re using V11 and not in a Public Workspace, you should see that.
Could you share more details about your OS and other environment setup, please?
In the meantime, you could use the same method here to reuse the scripts in your Collection.
https://www.postman.com/postman/postman-answers/collection/xz5vqwi/using-global-utility-functions?action=share&source=copy-link&creator=1794236
Store as a Global Variable:
utils = {
resetCollectionVars: (pm, prefix) => {
const variableKeys = Object.keys(pm.collectionVariables.toObject());
variableKeys.forEach((arrItem) => {
if (!arrItem.startsWith(`${prefix}`)) {
pm.collectionVariables.set(arrItem, "");
}
});
}
};
In your script:
eval(pm.globals.get("utils"));
utils.resetCollectionVars(pm, 'base_url')
This is a workaround but I would recommend using the Package Library once we sort out what’s happening there.
koustubh24
(Koustubh.K)
October 15, 2024, 8:31pm
13
Thank you @danny-dainton for that solution. As for the details, they’re as follows:
OS = Windows 10 Enterprise & I’m behind a secure firewall (Zscaler).
system
(system)
Closed
November 14, 2024, 8:32pm
14
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.