将环境本地化以便在Postman中进行收集 [英] Making an environment local for collection in Postman

查看:191
本文介绍了将环境本地化以便在Postman中进行收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





我有很多项目,每个项目都有它的集合,我想为每个项目设置不同的环境URL。但是,我看到所有集合都共享这些环境。我如何才能使集合本地化环境?



谢谢!

解决方案

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



其中有:




  • 全局变量

  • 环境变量

  • 集合变量

  • 局部变量

  • 数据变量



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



全局>收集>环境>本地>数据
-这就是分辨率的顺序。



因此,可以这样操作:



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



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



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



如果只想使用每个集合专用的变量,则需要使用集合变量



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



然后单击编辑>变量>添加只希望特定于此集合的变量。





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



或在测试脚本中:

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

邮递员会为您完成其余工作。



当您具有不同的环境(例如PROD,STAGING,BETA等)时,环境变量会派上用场。许多人都这样使用它们。
否则,它们只是变量,所以您可以随意使用它。



或者,如果您想将环境用于几个集合,将另一个环境用于另一个。
然后创建多个工作区,并使用相应的环境向其中添加集合。最后,根据需要在这些工作空间之间切换。


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.

这篇关于将环境本地化以便在Postman中进行收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆