如何设置Postman以使用pm.globals.set替代postman.setGlobalVariable?
pm.globals.set() Not Working in Postman Hey there! I totally get the confusion—switching between old and new Postman APIs can be tricky. Let's break down why pm.globals.set("id","12345") isn't working for you, and how to fix it.
The Root Cause
The pm.* API (including pm.globals.set()) was introduced in Postman v5.0 as the modern replacement for older methods like postman.setGlobalVariable(). If your Postman version is older than v5.0, the pm namespace won't be available at all. Even if you're on a slightly newer version, there might be compatibility bugs blocking it from working properly.
Step-by-Step Fix
Here's how to get up and running with the modern API:
Check your Postman version
Open Postman, click the Postman icon in the top-left corner, go toSettings→About. Look for the version number—if it's below v5.0, that's definitely the issue.Upgrade to the latest Postman version
Head over to the official Postman download page to grab the newest desktop version. After installing, restart Postman completely to ensure the new API loads properly.Verify the fix
Once upgraded, go to your Tests tab and enter:pm.globals.set("id", "12345"); // Add this line to confirm it worked console.log("Global variable set:", pm.globals.get("id"));Send your request, then check the Console (bottom-left of Postman) to see if the log shows the correct value. You can also verify manually by clicking the eye icon in the top-right to open
Globals—theidvariable should appear with the value "12345".
Bonus Note
The old postman.setGlobalVariable() method is now deprecated, meaning it might be removed in future versions. Using pm.globals.set() is better because it’s part of a more consistent, feature-rich API—you’ll also get access to handy extras like pm.globals.unset() to delete variables, or pm.environment for environment-specific storage.
内容的提问来源于stack exchange,提问作者colin_dev256

