Open In App

How to use variables and data files together in Postman ?

Last Updated : 08 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • Define Variables: You can define variables at a global level or specific to an environment.
  • Use in Requests: Reference these variables in your requests using double curly braces, like {{baseUrl}}.

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.

Screenshot-2023-12-29-110517

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

vari

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:

  • Step 1: Add a new request in the colletion, it should be a GET request.
  • Step 2: Now on the url write {{baseUrl}}/posts it will fetch the baseUrl from the environment variable that we have save earlier.
  • Step 3: Click on the send button, and we can see the responce in the image below.

Screenshot-2023-12-29-110701

POST Request:

Follow these steps to test the POST requests:

  • Step 1: Add a new request in the colletion, it will be a POST request.
  • Step 2: On the body section of this request add the following
{
"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.

  • Step 3: Now we have to use the Colletion runner to actually post these data from the data.json file that we created earlier.
  • Step 4: selection the three dot on the colletion and select the option Run colletion.

Screenshot-2023-12-29-111700

  • Step 5: Now upload that data.json file and run the Collection, it will show the result with several other details.

Screenshot-2023-12-29-111559

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 a new directory for your project.
  • Initialize it with command npm init It will create a package.json file.
  • Install Express.js with the command npm install express

Create an Express Server:

  • Create a file named app.js.
  • Implement basic GET and POST routes, use the code snippet below.

Javascript




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:

  • Open Postman.
  • Create a new environment.
  • Add a variable named baseUrl and set its value to your Express server URL (e.g., http://localhost:3000).
  • Create Collection and Add Requests

Create a new collection in Postman:

  • Add a GET request with URL {{baseUrl}}/posts.
  • Add a POST request with URL {{baseUrl}}/posts.
  • In the Body of the POST request, use:
{
"title": "{{title}}",
"body": "{{body}}",
"userId": "{{userId}}"
}

Using Collection Runner:

  • Open Collection Runner in Postman.
  • Select the created collection.
  • Upload the data.json file.
  • Run the collection to test the GET and POST requests with data from the file.

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads