Do you think IIT Guwahati certified course can help you in your career?
No
Introduction🤓
Postman is an API client that allows developers to create, share, test, and document APIs. This allows users to create, save, and read simple and complex HTTP/s requests.
The variables in postman are just like any other programming language. It will allow you to store and reuse values. The use of variables makes it easier to work efficiently, collaborate with teammates, and create active workflows.
This article will briefly discuss how to use variables in postman. We will start our discussion by knowing how to create and use variables in postman. Scope and types of variables. Afterward, we will discuss how to define the types of variables. At last, we will understand how variables in postman are used in sharing and persisting data. This will conclude our discussion on how to use variables in postman. So let’s get started!
What is a Variable?🤔
Variables here are key-value pairs. As each variable name represents its key. Pointing to the variable name gives you access to its value. Variables can be used to pass data between requests and tests. Such as when we chain requests in a group.
The use of variables improves the user's productivity and reduces errors.
Creating and Using Variables in Postman😃
🍁To create and use variables in Postman follow these steps,
Step 1: Select the quick look icon for the environment. The quick look icon is located in the upper right corner.
Step 2: Select Edit next to Globals.
Step 3: Create a variable called my_variable and set its initial value to Hello.
Step 4: Choose the Save icon. Save and then exit the tab.
Step 5: Open a new request tab and enter the URL https://postman-echo.com/get?var={{my_variable}}. Please hover your mouse over the variable name to see its value and scope.
Step 6: Select Send to send the request. Check the response to ensure that the variable value has been sent to the API.
Step 7: Change the setting in the quick look and resend the request.
Scope of Variables🕳️
In our discussion of how to use variables in Postman. Let's look at different scopes of variables here. A variable's scope is nothing more than its lifetime and accessibility. It's similar to the concept of scope found in programming languages such as Java.
A variable, for example, can have a global scope. This means that any class/method can access/modify the variable. At the same time, the method or program is active.
There can be different types of variables with a specific scope. Local variables declared in the method can only be used while that function is running. Variables declared for loops can only be used while the loop runs.
🍁Postman provides the following five types of variable scopes:
Global
Collection
Environment
Data
Local
Global Variables🌐
Global variables are general-purpose variables. It should be mostly avoided. It can only be used for quick prototyping needs. These variables are available to all requests known in the console.
These variables are similar to global scope variables in languages such as Java, C#, and others. Here these variables are accessible to all parts of the program. These variables are highly erratic because each code part can change the global variable value.
Collection Variables📄
Variables at the scope of the collection are defined using collection variables. The collection in Postman, as we all know, is nothing more than a group of Postman requests.
Collection variables do not change while a collection is executed. These variables can be retrieved. And it can not be updated during request execution.
Environment Variables📨
Environment variables are the most common type of variable in Postman.
They are linked to an environment that is used to carry out the request. They have a broader range than Global variables but a more limited range than Collection variables.
Local Variables📭
Local variables are only available within the range of request execution. These are identical to Function variables. Here, the variables can only be reached within the function execution range.
An application of local variables is when you want to override the values of variables. It can be defined in other scopes, such as Global, Collection, or Environment.
For example, if we have a global variable named 'testVar'. And you want to change its value without affecting the global variable. You can create a local variable with the same name and use it with whatever value you want.
Data Variables📃
Data variables appear during request execution through the collection runner.
Postman allows us to execute requests in a collection using the collection runner. During execution, we can provide a data set in the form of JSON or CSV. It can be used to run the requests within the collection.
It is important to note that the source of Data variables is the user-supplied data file in JSON or CSV format. Data variables can only be fetched but not updated/modified, or added during request execution.
Variable types🧐
In addition to scope, global and environment variables can be defined by type. You can make two types of variables for global and environment variables:
Default type: Variables are, by default, assigned the default type. This type is displayed as plain text. And it does not have any additional properties.
Secret type: The secret type conceals all workspace members' initial and current values. And it can be used to prevent the unintentional exposure of sensitive data such as API secrets, passwords, tokens, and keys.
🍁To change the variable type to secret. Do the following:
Step 1: Select the environment quick look icon 👁️ in Postman's top right corner.
Step 2: Select Edit to open the editor for the environment or global variables.
Step 3: To open the dropdown. Select default next to the variable you want to change. Then select secret to update the variable type.
Step 4: To confirm your changes, click the Save icon.
Defining variables💫
Whether you need global, environment, or collection scope. You can define variables in various ways.
In the request builder, you can also define variables at any scope:
Step 1: Choose the data you need. Such as in the address, parameters, headers, or body. And then select Set as variable.
Step 2: Now select Set as a new variable option.
Step 3:Enter the Name. Confirm whether the Value is correct and select a scope. Then choose Set Variable.
Adding and Modifying Global Variables💻
🍁To add a new global variable. Do the following:
Select Add a new variable. And give the variable a name.
For the new variable, choose a Type.
Add an Initial Value. And, if desired, a Current Value.
To confirm your changes, click the Save icon.
🍁To modify an existing global variable. Use the following steps:
Change the value of the desired variable.
To confirm your changes, click the Save icon.
Downloading Global Environments
🍁To download global variables as JSON. Follow these steps:
In the sidebar, click Environments.
Select Globals.
Select Export.
Select Save after deciding where to save the file.
Adding and Modifying Environment Variables🖥️
🍁To add a new environment variable. Do the following:
Select Add a new variable. And give the variable a name.
For the new variable, choose a Type.
Add an Initial Value. And, if desired, a Current Value.
To confirm your changes, click the Save icon.
🍁To modify an existing environment variable. Use the following syntax:
Change the value of the desired variable.
To confirm your changes, click the Save icon.
Defining Collection Variables🖥️
You can add collection variables when you first create the collection or later.
🍁To add or change a variable in an existing collection. Do the following:
In the sidebar, click Collections.
Select the group and then the Variables tab.
Using Variables🧑💻
Double curly braces can be used to refer to variables in Postman. Refer to a variable named "username" in your request settings. For example, use the following syntax with the double curly braces around the username:
{{username}}
Postman will fix the variable. And it will replace it with its current value when you run a request.
For example, you have a request URL. That references a variable like this:
Postman will send whatever value you have stored. For the cust_id variable when the request is executed. If the current cust_id is 3, the request will be routed to the following URL. To include the query parameter:
To access a variable from within a request body. Have its reference in double-quotes:
{ "customer_id" : "{{cust_id}}" }
Using Dynamic Variables😃
Postman also provides dynamic variables that you can use in your requests. It generates sample data using the faker library. It includes random names, addresses, email addresses, and other information. These pre-defined variables can be used multiple times to return different values per request.
You can use these variables in Postman just like any other variable. Their values are generated during execution, and their names begin with a $ symbol, such as $guid or $timestamp.
🍁Some examples of dynamic variables are the following:
🍁Some examples for text, numbers, and colors:
Sharing and persisting data🤓
When you edit Postman's global, collection, or environment variables. You can choose whether to Persist or Reset the current value. To apply this setting to all variables, select Persist All or Reset All. These allow you to control what happens within your local Postman instance. Regardless of how the data is synced with others who share your workspace, requests, collections, and environments.
You can enter both an initial and current value when creating or editing a variable. If we leave the current value empty when creating a new variable in Postman. It will autofill with the initial value. If we specify a present value, it will be instance-specific. The Persist option allows you to push your current value to shared data. You are updating the initial value to match.
Frequently Asked Questions✍️
What are the variables in Postman?
Postman variables are key-value pairs. Because each variable name represents its key. Referencing the variable name gives you access to its value. Variables can be used to pass data between requests and tests, such as when chaining requests in a collection.
In Postman, how will you log variable values?
Select "View" and then "Show Postman Console" from the application menu. You can use console.log(foo). In your script to log the value of a variable. When you send a request, the script is evaluated. And the variable's value is logged in the Postman Console.
How do I give Postman JSON data?
Change the method next to the URL in Postman to 'POST.' And then, under the 'Body' tab, select the 'raw' radio button and then 'JSON (application/json)' from the drop-down. You can now enter the JSON that will be sent with the POST request. If everything goes well, you should see the new data in your 'db. JSON' file.
Conclusion📄
In this article, we have extensively discussed how to use variables in Postman. We started by getting an understanding of variables in Postman. And how to create and use these variables. Afterward, we briefly discussed the scope of variables in Postman. Defined variables and added and modified different types of variables according to our needs.
At the end of the article, we discuss the use of variables. Including the use of dynamic variables and sharing and persisting data. Concluding our discussion of how to use variables in Postman.
If you think this blog has helped you with the topic of how to group requests in collections in Postman. And if you would like to learn more, check out our articles Introduction to Postman, Integration in Postman, Rest API, API testing, API, and Web Testing. You can also refer to our guided path on the basics of java and many more on our Website.
But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundles for placement preparations.
Nevertheless, you may consider our paid courses to give your career an edge over others!