【问题标题】:Postman: How to delete/clear postman environment variable at run-timePostman:如何在运行时删除/清除 postman 环境变量
【发布时间】:2017-08-31 23:38:05
【问题描述】:

有没有办法在运行时使用函数删除/清除邮递员环境变量。 我可以设置为空白或一些特殊值,但有没有通用的做事方式。

【问题讨论】:

    标签: postman


    【解决方案1】:

    沙盒 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 不同(它只是清除环境值)
    【解决方案2】:

    好的 - 这样就可以了。

    postman.clearEnvironmentVariable("key");
    

    【讨论】:

    • postman.clearEnvironmentVariables() 将立即删除所有环境变量。
    【解决方案3】:

    正确的语法是:

    pm.environment.unset("key");
    

    【讨论】:

    • 答案中也提到了这一点。此外,这不是正确的语法,而是正确的答案之一。这是一个 pm api,但邮递员也提供了自己的包装器 clearEnvironmentVariable
    【解决方案4】:

    首先在邮递员 UI 中选择环境 然后选择单击三个点 选择删除环境变量

    【讨论】:

    • 这个想法是在运行时清除它 - 您的回复更像是设计时编辑,但感谢您的详细说明
    【解决方案5】:

    我也遇到了将变量设置为空白的这种可能的解决方案: 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();
    

    【讨论】:

      猜你喜欢
      • 2018-11-14
      • 2018-09-30
      • 2020-08-06
      • 2021-08-11
      • 1970-01-01
      • 2016-09-24
      • 2014-02-20
      • 2016-07-22
      • 1970-01-01
      相关资源
      最近更新 更多