一、语法
1、从环境变量获取值
例:从环境变量env获取值
pm.environment.get("env");
二、例子
const xml2js = require('xml2js');
const env = pm.environment.get("env");
if (env === "localhost") return;
const eurekaUrl = pm.environment.get("eureka_url");
const gray = pm.environment.get("env");
let serverName = "best-gw-test";
if (env === "prod") {
serverName = "best-gw";
}
pm.sendRequest(eurekaUrl + `/${serverName}`, (err, response) => {
xml2js.parseString(response.text(), (e, result) => {
const list = [];
result.application.instance.map(item => {
const metadata = item.metadata[0];
const deployflag = metadata.deployflag ? metadata.deployflag[0] : metadata.envflag[0];
let url = metadata["management.url"] ? metadata["management.url"][0] : item.homePageUrl[0];
url = url.substring(0, url.length-1);
list.push({url: url, gray: deployflag});
});
let s = list.find(item => item.gray === gray);
if (!s) {
s = list[0];
}
console.log(`best gw url: ${s.url}`);
pm.environment.set("best_gw_url", s.url);
});
});