How To Use Variables In Postman Body
Using Variables in Postman Body for API Testing
Variables play a crucial role in API testing by making your requests more dynamic and reusable. Postman offers various ways to define and use variables within the body of your requests, enabling you to handle different scenarios and data effortlessly. Let’s explore some common techniques:
1. Defining Variables in the Postman UI
The simplest way to define variables is through the Postman UI itself.
1.1 Environment Variables
Step 1: Navigate to the “Environment” tab in Postman. Step 2: Click on the “Add” button to create a new environment. Step 3: Give your environment a name, for example, “MyTestEnv.” Step 4: In the “Variables” section, add a variable with a name, like “username” and a value, such as “testuser.”
Example:
{ "username": "{{username}}", "password": "{{password}}"}
Step 5: Choose the environment “MyTestEnv” from the dropdown at the top of the Postman UI before sending your request. The placeholders {{username}}
and {{password}}
will be replaced with the respective values from your environment.
1.2 Global Variables
Global variables are accessible across all your Postman collections and environments.
Step 1: Click the “Global” tab in the environment section. Step 2: Follow steps 3-4 from Environment Variables section to define your global variables.
2. Using Variables in Code Snippets
Postman’s scripting capabilities allow you to define and manipulate variables within code snippets.
2.1 Using Pre-Request Scripts
Step 1: Go to the “Pre-request Script” tab in your request.
Step 2: Use pm.environment.get()
or pm.globals.get()
to fetch existing environment or global variables.
Step 3: Store the fetched values in variables using let
or const
.
Step 4: Use these variables within your request body.
Example:
let userId = pm.environment.get('userId');let timestamp = Date.now();
pm.test("Verify user ID", () => { pm.expect(userId).to.be.a('string');});
pm.request.body.raw = JSON.stringify({ "userId": userId, "timestamp": timestamp});
3. Data-Driven Testing with Collections
Postman Collections allow you to organize and manage your API requests. They offer powerful features for data-driven testing by using data files to populate variables in your request body.
3.1 Importing Data Files
Step 1: Create a collection in Postman and add your request. Step 2: Go to the “Data” tab in your collection. Step 3: Choose a data file format like CSV, JSON, or XML. Step 4: Import your data file, containing rows of variable values.
Sample Data File (JSON):
[ { "username": "user1", "password": "pass1" }, { "username": "user2", "password": "pass2" }, { "username": "user3", "password": "pass3" }]
3.2 Using Variables in the Body
Step 1: Replace the hardcoded values in your request body with variables like {{username}}
and {{password}}
.
Step 2: Run the collection with the “Runner.” Each iteration will pick a row from your data file and populate the variables in your request.
4. Dynamically Generating Data
You can use JavaScript code in the Pre-Request Script to generate data dynamically and assign it to variables used in your request body.
Example:
let randomString = Math.random().toString(36).substring(2, 15);
pm.request.body.raw = JSON.stringify({ "name": `Test User ${randomString}`});
This code generates a random string and uses it in the name
field of the request body.
Conclusion
Using variables effectively in Postman body can significantly enhance your API testing efficiency and flexibility. Leveraging environments, globals, pre-request scripts, and data-driven testing with collections empowers you to create sophisticated and reusable test scenarios. Experiment with different techniques and find the best approach for your specific testing needs.