我可以撰写变量以在邮递员中创建新的Varibales吗?

发布于 2025-02-13 00:30:54 字数 433 浏览 0 评论 0原文

我的变量为base_url。当我使用localhost时,我将使用一个更多的变量端口构成它,该端口反映了服务在我本地上运行的端口。这两个变量组成为{{base_url}}:{{port}}定义了我的API的完整基础URL。

但是,在测试我的API的已部署版本时,我的基本URL只是https://www.xyzapi.com,而没有任何端口明确声明。

我正在使用{{base_url}}/rule-service/v1/find-by-txn格式在请求URL中。我正在使用环境在local远程之间切换。

如何在两种情况下都使用相同的请求格式?我有多个在不同端口上运行的微服务。

I have a variable as BASE_URL. When I am using the localhost, I am composing it with one more variable PORT that reflects the port on which the service is running on my local. These two variables composed as {{BASE_URL}}:{{PORT}} defines the complete base URL for my APIs.

But when testing the deployed version of my API, my base URL is just https://www.xyzapi.com without any port declared explicitly.

I am using {{BASE_URL}}/rule-service/v1/find-by-txn format in the request URL. I am using environments to switch between local and remote.

How can I utilize the same request format for both cases? I have multiple microservices running on different ports.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁吢 2025-02-20 00:30:55

该代码完成了工作!

let baseUrl = pm.environment.get("BASE_URL");

if(baseUrl.includes('localhost')){
    let port = pm.collectionVariables.get("PORT");
    baseUrl = baseUrl.split(':')[0];
    baseUrl = `${baseUrl}:${port}`;
}

pm.environment.set("BASE_URL", baseUrl);

This code did the job!

let baseUrl = pm.environment.get("BASE_URL");

if(baseUrl.includes('localhost')){
    let port = pm.collectionVariables.get("PORT");
    baseUrl = baseUrl.split(':')[0];
    baseUrl = `${baseUrl}:${port}`;
}

pm.environment.set("BASE_URL", baseUrl);
慕烟庭风 2025-02-20 00:30:55

尝试一下。它引入了variabe final_base_url,该将用于构建您的URL(例如{{final_base_url}}/rule-service/rule-service/v1/find-by-txn)包括端口是否,取决于设置环境变量port

if (pm.environment.get("PORT")) { 
    let FINAL_BASE_URL = pm.environment.get("BASE_URL") + ":" + pm.environment.get("PORT")
} else {
    let FINAL_BASE_URL = pm.environment.get("BASE_URL")
}

Try this. It introduces a variabe FINAL_BASE_URL which is to be used to build your URLs (eg {{FINAL_BASE_URL}}/rule-service/v1/find-by-txn) and it includes the PORT or not, depending if an environment variable PORT is set.

if (pm.environment.get("PORT")) { 
    let FINAL_BASE_URL = pm.environment.get("BASE_URL") + ":" + pm.environment.get("PORT")
} else {
    let FINAL_BASE_URL = pm.environment.get("BASE_URL")
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文