Open In App

How to use variables and data files together in Postman ?

In this article, we are going to learn the process of using variables and data files together in the Postman. Postman is a powerful tool for API testing, offering extensive capabilities for automating tests and handling various API request types. One of its key features is the ability to use variables and data files together, which greatly enhances the flexibility and scalability of your tests. This article covers how to use variables and data files in Postman, addressing all common types of API requests: GET, POST, PUT, and DELETE.

Variables and Data Files in Postman:

Variables: In Postman, variables can store data that you can reuse across different requests and environments. Variables can be defined at various scopes:

Data Files: Postman allows you to drive your tests with data from external files in JSON or CSV formats. Each row in these files can represent a different set of data to be used in a series of requests.



Setting Up Variables:

Follow these steps to set the variables:

Step 1: Open the Postman
Step 2: Create or Select a collection, then go to the variables tab and name the variable baseUrl and on the value put this URLvariables https://jsonplaceholder.typicode.com then save it.

Step 3: Now we have successfully created a varialbe, and for our testing purpose we can use this variable to test the request.

Preparing Data Files:

Format: Structure your JSON or CSV files with key-value pairs representing the data you want to use in your requests. For this article, we will be using demo api to test both variables and data files.

Create a data.json file and put the given values as shown in the image below, save this file in the local disk we will use it later.

data.json:

[
{
"title": "First Post",
"body": "This is the first post body",
"userId": 1
},
{
"title": "Second Post",
"body": "This is the second post body",
"userId": 2
},

"title": "Third Post",
"body": "This is the third post body",
"userId": 3
}
]

Using Variables and Data Files in Different Requests:

Now we have setup our variable and prepared our demo data, now we can test those together on different request, so lets do that one by one.

GET Request:

Follow these simple steps to test the GET request:

POST Request:

Follow these steps to test the POST requests:

{
"title": "{{title}}",
"body": "{{body}}",
"userId": "{{userId}}"
}

Now here we are using some variables, title, body and userId since it is wrapped on the curly braces.

Implementation using Express:

Now we will implement the use of variables and data files both in the Postman with the Express as a backend. So we have to create a simple Express applicaion on which we will make sure that it can support the GET and POST requests. Then we will use the Postman to test the endpoints. There are some simple steps that we have to follow to make this work, These steps are as follows:

Step 1: Setting Up Express Application

Initialize Node Project:

Create an Express Server:




const express = require('express');
const app = express();
 
app.use(express.json());
 
// GET route
app.get('/posts', (req, res) => {
    res.json({ message: "Fetching posts..." });
});
 
// POST route
app.post('/posts', (req, res) => {
    const { title, body, userId } = req.body;
    res.json({ title, body, userId });
});
 
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

Run the Express Server

Run the server by running the command in the terminal

node app.js

Step 2: Preparing Data Files for Postman

Create a data.json File in the same directory, this file will contain the data to be used in POST requests. Paste the following data in the data.json file.

[
{
"title": "First Post",
"body": "This is the first post body",
"userId": 1
},
{
"title": "Second Post",
"body": "This is the second post body",
"userId": 2
},
{
"title": "Third Post",
"body": "This is the third post body",
"userId": 3
}
]

Using Postman:

Set Up Environment Variables:

Create a new collection in Postman:

{
"title": "{{title}}",
"body": "{{body}}",
"userId": "{{userId}}"
}

Using Collection Runner:

Conclusion: In conclusion, we explored the integration of variables and data files in Postman for dynamic API testing, demonstrating how to efficiently handle various request types like GET and POST. This approach significantly enhances test flexibility and scalability, enabling a more streamlined and effective API testing process. By utilizing environment variables and data-driven testing methods, Postman proves to be an invaluable tool for modern API testing scenarios.


Article Tags :