1. Overview
1.概述
In Postman, variables let us save the values and use them as required in different APIs. We can reference these values by passing them to collections, environments, or any requests. It basically makes it easier to access stored values using these variables more efficiently. We can also collaborate with multiple teammates or set up dynamic workflows over Postman using these variables.
在Postman中,变量让我们保存数值,并根据需要在不同的API中使用它们。我们可以通过将这些值传递给集合、环境或任何请求来引用它们。它基本上使我们更容易使用这些变量更有效地访问存储的值。我们还可以与多个队友协作,或者通过Postman使用这些变量设置动态工作流。
In this article, we’ll understand variables in-depth, their types, and how we can embed them into our APIs.
在这篇文章中,我们将深入了解变量,它们的类型,以及我们如何将它们嵌入我们的API中。
2. What Are Variables?
2.什么是变量?
Variables symbolize the data representation and let us access a value without manually entering it wherever we need it. They can be useful when we’ve to use the same value in multiple places. Some instances include base URL, token, and path variables. Further, using them also makes the requests more flexible as changing variables will automatically make changes at all places these variables are used.
变量象征着数据表现,让我们无需在需要的地方手动输入数值就能访问它。当我们必须在多个地方使用相同的数值时,它们会非常有用。一些例子包括基本URL、token和路径变量。此外,使用这些变量还可以使请求更加灵活,因为改变变量将在所有使用这些变量的地方自动进行改变。
We can define variables as key-value pairs in Postman. The key defines the variable name that allows us to access its value directly using this key.
我们可以在Postman中把变量定义为键-值对。键定义了变量名称,允许我们使用这个键直接访问其值。
In addition, one can also use environments for setting up a group of variables. These variables will have different values depending on the environment.
此外,人们还可以使用环境来设置一组变量。这些变量将根据环境的不同而有不同的值。
Let’s dive deep into the variables and understand the different variable scopes.
让我们深入研究变量,了解不同的变量作用域。
2.1. Variable Scopes
2.1.可变范围
There are multiple scopes that let us process the development and testing of API in various environments with different values. Below are the variable scopes defined from the broadest to the narrowest area covered:
有多个作用域,让我们在不同的环境中用不同的值来处理API的开发和测试。下面是定义的变量范围,从最广泛到最狭窄的覆盖区域。
- Global variables: These are accessible throughout the workspace and have the broadest scope in Postman. They can be used anywhere among multiple requests and collections within the workspace.
- Collection variables: These variables are accessible only inside a certain collection. They are available across all the requests within a collection. Also, they don’t change based on the selected environment.
- Environment variables: These variables let us scope the work as per the different environments. They change along with the change in the environment we are working on like local environment, staging or the production environment.
- Data variables: These types of variables are external and define the data sets while running collections with the Collection Runner. We can extract this from a CSV or a JSON file. They have current values that don’t persist after a request or collection executes.
- Local variables: These variables are also known as temporary variables that are only accessible through a request script. They have scope only till the current request or collection. Once the execution completes, they are no longer available.
2.2. Variable Types
2.2.变量类型
Global and Environment variables can further be classified based on their types. Here’s how we can configure them:
全局变量和环境变量可以根据其类型进一步分类。下面是我们如何配置它们。
- Default type: Variables are of these types by default. These are shown as plain text with no additional properties.
- Secret type: These variables hide the initial and current values for all the workspaces, similar to a password. They let us prevent any unintentional disclosure of sensitive data.
3. Defining Variables With Scopes
3.用作用域定义变量
We can define variables in multiple types and with scopes as we discussed above. Variables can be defined at any scope in the request builder.
我们可以用多种类型和范围来定义变量,正如我们上面所讨论的。变量可以在请求生成器的任何范围内定义。
Firstly, we select the text and click on Set as a variable. Once done, we have to store it as a new variable. Then, we enter a key for the value and select from the different scopes available in Postman:
首先,我们选择文本并点击Set as a variable。一旦完成,我们必须将其存储为一个新的变量。然后,我们为该值输入一个键,并从Postman的不同作用域中选择。
3.1. Setting the Response Body as Variables
3.1.将响应主体设置为变量
The values from the request’s response body could also be set up into variables and used further in the collection. This helps in running multiple requests in sequence and creating a specific flow.
来自请求的响应体的值也可以被设置成变量,并在集合中进一步使用。这有助于依次运行多个请求并创建一个特定的流程。
Firstly, let’s select the text we want to save and press right-click or the Control-click button to store it as a variable.
首先,让我们选择我们要保存的文本,然后按右键或控制键按钮,将其存储为一个变量。
Once done, we have to choose among the relevant scope from the possible options, i.e., environment, local or global, and name the variable to save it:
一旦完成,我们必须从可能的选项中选择相关的范围,即环境、本地或全局,并命名变量以保存它。
3.2. Defining Global Variables
3.2.定义全局变量
Let’s look at the steps for setting up global variables in Postman:
我们来看看在Postman中设置全局变量的步骤。
- We start by clicking on Add a new variable in the global variable section, and then entering the variable name to store it as a global variable directly.
- Another option is to select the value and store the variable by choosing the Global option as Type.
- Once done, we shouldn’t forget to mark Save and confirm the changes.
We can also download the global variables and share them with others by going through these steps:
我们也可以通过这些步骤下载全局变量并与他人分享。
- First, let’s go to Postman and select the environment type as Global in the sidebar.
- Then, we can click on the Export button that will export the variables to a file.
- We can store the file once ready by clicking Save on the system, and sharing it as required.
3.3. Defining Environment Variables
3.3.定义环境变量
We can go through the following steps to save environment variables in Postman:
我们可以通过以下步骤来保存Postman中的环境变量。
- Firstly, we click on the Add a new variable, and Enter the variable name.
- Before saving the variable, we can select the variable type as the environment.
- The variable will need initial and current values to be specified.
- Once done, we have to click on Save to confirm the changes.
3.4. Defining Collection Variables
3.4.定义集合变量
Postman also provides the opportunity to add collection variables and use them as required.
Postman还提供了添加集合变量的机会,并根据需要使用这些变量。
We have to click and select Collections in the sidebar. Once done, let’s select the Variables tab to store the collection variables:
我们必须点击并选择侧边栏中的Collections。一旦完成,让我们选择Variables标签来存储集合变量。
3.5 Defining Variables in Scripts
3.5 在脚本中定义变量
Similar to defining variables in a collection, an environment, or globally, we can also set variables programmatically in our request scripts.
与在集合、环境或全局中定义变量类似,我们也可以在请求脚本中以编程方式设置变量。
All of these methods take in (variable_key, variable_value) as input:
所有这些方法都接受(variable_key, variable_value)作为输入。
- pm.globals: This method is used for defining the global variables in a request script, e.g., pm.globals.set(“variable_key”, “variable_value”);
- pm.collectionVariables: We can define a variable with scope as ‘collection’ with this method, e.g., pm.collectionVariables.set(“variable_key”, “variable_value”);
- pm.environment: This can be used for defining an environment variable with scope as current environment, e.g., pm.environment.set(“variable_key”, “variable_value”);
- pm.variables: This defines the local variable with local/temporary scope, e.g., pm.variables.set(“variable_key”, “variable_value”);
- unset: This method can be used for removing a set variable. The unset can be defined with the above variable instances as per their scope, e.g., pm.environment.unset(“variable_key”, “variable_value”);
3.6 Using Variables in Scripts
3.6 在脚本中使用变量
We’ve stored the variables in scripts by using the above methods. Therefore, we can retrieve their current value using these methods:
我们已经通过上述方法将这些变量存储在脚本中。因此,我们可以用这些方法检索它们的当前值。
- pm.variables.get(“variable_key”): This will access a variable at any scope including the local.
- pm.globals.get(“variable_key”): This can access a global variable
- pm.collectionVariables.get(“variable_key”): This can access a collection variable.
- pm.environment.get(“variable_key”): This can access an environment variable.
Here, we can retrieve the values using variable_key. The object represents the scope level and the get() method retrieves the value.
在这里,我们可以使用variable_key来检索值。该对象代表了范围级别,get()方法检索了该值。
The pm.variables.get() method also provides the option to change variable scopes without actually affecting the script functionality. It returns the variable that currently has the highest precedence.
pm.variables.get()方法也提供了在不影响脚本功能的情况下改变变量范围的选项。它返回当前具有最高优先级的变量。
4. Using Variables
4.使用变量
Now, we’ve defined different variable scopes and learned how to store them in Postman. Let’s see how to use these variables in our request paths and bodies.
现在,我们已经定义了不同的变量范围,并学会了如何在Postman中存储它们。让我们看看如何在我们的请求路径和正文中使用这些变量。
For referencing a variable throughout Postman, we should use double curly braces around the variable’s name. It automatically fetches the value from different variables as defined. This is standard across all of Postman and doesn’t depend on variable types and scopes:
对于在整个Postman中引用一个变量,我们应该在变量名称周围使用双大括号。它自动从不同的变量中获取定义的值。这是所有Postman的标准,不依赖于变量类型和作用域。
{{studentName}}
Additionally, running CURL or hitting the request resolves the variable and updates the variable with the current value. The below request URL references the variable:
此外,运行CURL或点击请求会解析该变量,并以当前值更新该变量。下面的请求URL引用了该变量。
http://localhost:8080/get?student_name={{studentName}}
Let’s assume studentName is “John”. Postman retrieves the value from its variables and sends whatever value that’s currently stored there. Therefore, on hitting the above request over Postman it automatically converts it to the request:
让我们假设studentName是 “John”。Postman从其变量中检索该值并发送当前存储在那里的任何值。因此,在将上述请求打到Postman上时,它自动将其转换为请求。
http://localhost:8080/get?student_name=John
5. Conclusion
5.总结
In this tutorial, we used Postman variables for basic initialization using the GUI mode.
在本教程中,我们使用Postman变量进行基本初始化,使用GUI模式。
In addition, we learned about their types and scopes. Also, we looked at the different places they can be initialized such as URIs, request body, and headers. Evidently, this lets us save time and space by not initializing the same values multiple times.
此外,我们还了解了它们的类型和作用域。此外,我们还看了它们可以被初始化的不同地方,如URI、请求体和头文件。显而易见,这让我们不必多次初始化相同的值,从而节省时间和空间。