【发布时间】:2017-08-31 23:38:05
【问题描述】:
有没有办法在运行时使用函数删除/清除邮递员环境变量。 我可以设置为空白或一些特殊值,但有没有通用的做事方式。
【问题讨论】:
标签: postman
有没有办法在运行时使用函数删除/清除邮递员环境变量。 我可以设置为空白或一些特殊值,但有没有通用的做事方式。
【问题讨论】:
标签: postman
沙盒 API pm.environment.unset(variableName) 也允许这样做。
如果您想一次清除所有环境变量 - 您可能需要这样做:pm.environment.clear()。
这将清除所有环境值。
参考:https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/#pmenvironment
【讨论】:
pm.globals.clear(); 将清除所有全局变量
pm.environment.clear() 不仅会清除环境值,还会清除所有变量。这与在所有环境变量上运行 unset 不同(它只是清除环境值)
好的 - 这样就可以了。
postman.clearEnvironmentVariable("key");
【讨论】:
postman.clearEnvironmentVariables() 将立即删除所有环境变量。
正确的语法是:
pm.environment.unset("key");
【讨论】:
clearEnvironmentVariable
首先在邮递员 UI 中选择环境 然后选择单击三个点 选择删除环境变量
【讨论】:
我也遇到了将变量设置为空白的这种可能的解决方案: https://community.postman.com/t/can-i-clear-just-the-current-values-in-an-environment/6176
我发现使用pm.environment.clear() 或pm.environment.unset(variableName) 会从环境中删除变量。但有时我想保留变量并清除值,例如我想与其他人共享环境,但它包含仅适用于我自己的值(例如 oauth 凭据)。
所以我重新使用了链接中描述的函数并使用pm.environment.set(variableName, "") 而不是未设置。
所以我的函数看起来像:
function clearVariables() {
// Get all the names of our env variables and put them in an array
const environmentVariables = pm.environment.values.map(function(variable) {
return variable.key;
});
// Filter through the above array but don't add variables as per conditions
const binTheseVariablesOff = environmentVariables.filter(function(variable) {
return !variable.toLowerCase().includes("auth");
});
// Now go through this new array and clear these env variables
return binTheseVariablesOff.forEach(function(variableName) {
pm.environment.set(variableName, "");
});
}
// Call the function
clearVariables();
【讨论】: