在 Postman 中创建本地收集环境环境、Postman

2023-09-07 10:50:17 作者:何曾顾及我@

我看过邮递员关于环境的文档:

怎么创建 本地连接

我有很多项目,每个项目都有自己的集合,我想为每个项目设置不同的环境 url.但是,我看到环境是通过所有集合共享的.如何使环境成为集合的本地环境?

谢谢!

解决方案

您发布的图片实际上定义了如何解析变量.你可以有不同类型的变量.

分别是:

全局变量环境变量集合变量局部变量数据变量

假设您有一个变量a",其值为 5,定义为全局变量.但是你也有一个 collection variable 'a' 值 '7'因此,当您发送请求时,解析变量的顺序就是图片告诉您的.

全局 > 集合 > 环境 > 本地 > 数据- 这是解决的顺序.

那么,就这样完成吧:

全局(a = 5)> 集合(a = 7)> 环境(a 未定义)> 本地(a 未定义)> 数据(a 未定义)

因此,经过解析顺序后,'a' 的最终值将是 7

环境变量可用于工作区中的所有集合每个环境都特定于工作空间.全局变量也是如此.

如果您只想使用特定于每个集合的变量,那么您需要使用 集合变量

要添加集合变量,只需转到侧边栏并将鼠标悬停在要为其添加变量的集合上,然后单击●●●"

然后点击编辑">变量">添加您只需要特定于该集合的变量.

现在在请求中,您可以像使用其他变量一样使用这些变量.例如:https://{{url}}/get?foo=bar

或在测试脚本中:

console.log(pm.variables.get('car'));//'阿斯顿·马丁'

邮递员会为您完成剩下的工作.

当您拥有不同的环境(例如 PROD、STAGING、BETA 等)时,环境变量会派上用场.很多人都这样使用它们.否则,它们只是变量,因此请随意使用.

或者,如果您想为几个集合使用一个环境,而为另一个集合使用另一个环境.然后创建多个Workspaces,并为其添加对应环境的集合.最后,根据需要在这些工作区之间切换.

I've seen the postman doc about environments:

https://www.getpostman.com/docs/v6/postman/environments_and_globals/variables

where an image expresses that the enviornments are local to a collection, which I'm not seeing that happens in my Postman.

I have many projects, each one with its collecion, and I want to set the different environment urls for each project. However, I'm seeing that the environments are shared through all the collections. How can I make the environments local to a collection?

Thank you!

解决方案

The picture that you posted actually defines as to how the variables are resolved. You can have different types of variables.

Which are:

Global Variables Environment Variables Collection Variables Local Variables Variables from data

Suppose you have a variable 'a' with a value 5 defined as a global variable. But you also have a collection variable 'a' with value '7' So, when you're sending the request, the order in which the variables will be resolved is what the picture tells you.

Globals > Collection > Environment > Local > Data - That's the order of resolution.

So, that'll be done like so:

Globals (a = 5) > Collection (a = 7) > Environment (a not defined) > Local (a not defined) > Data (a not defined)

So, the final value of 'a' after going through the resolution order would come out to be 7

Environment variables are available to all the collections in a Workspace Each environment is specific to the workspace. So are the globals.

If you want to use variables only specific to each collection then you need to use the Collection Variables

To add collection variables, just go to the sidebar and hover on the collection you want to add the variables for and click '●●●'

Then click 'Edit' > 'Variables' > Add the variables that you want only specific to this collection.

Now in the Request you can use these variables just in a similar fashion as you use the other variables. For eg.: https://{{url}}/get?foo=bar

or in a test script:

console.log(pm.variables.get('car'));  // 'astonMartin'

Postman will do the rest for you.

Environment variables come handy when you have different environments such as PROD, STAGING, BETA etc. Many people use them like so. Otherwise, they're just variables so use it as you like.

Alternatively, if you want to use an environment for a few collections and another environment for another one. Then create multiple Workspaces and add collections to it with the corresponding environment. Finally, switch between these workspaces as per the need.