Step By Step Working with Dynamic Variables
Step 1: Setup Node App
Open VScode and type the following command. Remember to create a separate folder for this project, and to name it like Postman_Dynamic_Variables so that you can check it whenever you need in the future. Navigate to the installsPostman_Dynamic_Variables folder.
touch index.js
This creates a new file with the name index.js in the current folder. Open terminal and type:
npm i express nodemon
This installs, express and nodemon packages, express helps us to create APIs and work with them and nodemon helps us to run the app with hot-reload capability. Make sure you are connected to the internet.
Upon successful run,, this command creates a folder node_modules, package.json and package-lock.json in the current directory. This is expected; now follow along and run in the terminal:
npm init -y
Now open the the index.js file and paste the following code; here we have written a sample express app with a plain POST route.
Step 2: Write the Core App Logic
index.js
Node
const app = express();
app.post(‘/’, (req, res) => {
res.send(‘Hello, welcome to my Express app!’);
});
const port = 8080;
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
Now open package.json and paste the following line under the the scripts section:
“start”: “nodemon index.js”
Now your package.json should look like this:
Node
"dependencies": {
"express": "^4.18.2",
"nodemon": "^3.0.2"
},
"name": "pdv",
"version": "1.0.0",
"main": "index.js",
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": ""
}
Step 3: Run the App
Now open a terminal and type:
npm run start
You should be able to see something like this:
To check if your app is accessible from Postman, open a new tab and type the following URL, you should be seeing Hello, welcome to my Express app!
http://127.0.0.1:8080/
Note:
127.0.0.1 is the shownsame as localhost and remember the method type is POST.
Step 4: Write the API to Serve Requests
Now we will modify our index.js file to store the employee details temporarily in the server’s RAM as shownlet’s below:
Node
const app = express();
app.use(express.json());
let employeeDetails = {};
// uniqueId – $guid
// createdAt – $timestamp
// defaultPassword – $randomPassword
// firstName – $randomFirstName
// lastName – $randomLastName
// prefix – $randomNamePrefix
// location – $randomJobArea
// designation – $randomJobTitle
// responsibilities – $randomJobDescriptor
// contact – $randomPhoneNumber
// city – $randomCity
// country – $randomCountry
// profile – $randomPeopleImage
// catchPhrase – $randomCatchPhrase
// joinedOn – $randomDateRecent
// email – $randomEmail
// department – $randomDepartment
// bio – $randomLoremText
app.post(‘/’, (req, res) => {
try {
const tempEmployeeDetails = req.body.employeeDetails;
console.log("tempEmployeeDetails >>>>", tempEmployeeDetails);
if (tempEmployeeDetails) {
employeeDetails[tempEmployeeDetails["uniqueId"]] = {
…tempEmployeeDetails,
}
res.send(employeeDetails);
}
} catch (error) {
console.error("Error occurred in processing your request", error);
res.send(‘BAD REQUEST!!’);
}
});
const port = 8080;
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
Step 5: Hit the API and Save Data
Now let’spostman’s try to hit this router and see if the details are being saved successfully or not.
The following is the postman’san body:
Javascript
{ "employeeDetails" : { "uniqueId" : "{{$guid}}" , "createdAt" : "{{$timestamp}}" , "defaultPassword" : "{{$randomPassword}}" , "firstName" : "{{$randomFirstName}}" , "lastName" : "{{$randomLastName}}" , "prefix" : "{{$randomNamePrefix}}" , "location" : "{{$randomJobArea}}" , "designation" : "{{$randomJobTitle}}" , "responsibilities" : "{{$randomJobDescriptor}}" , "contact" : "{{$randomPhoneNumber}}" , "city" : "{{$randomCity}}" , "country" : "{{$randomCountry}}" , "profile" : "{{$randomPeopleImage}}" , "catchPhrase" : "{{$randomCatchPhrase}}" , "joinedOn" : "{{$randomDateRecent}}" , "email" : "{{$randomEmail}}" , "department" : "{{$randomDepartment}}" , "bio" : "{{$randomLoremText}}" } } |
Output:
Postman Dynamic Variables
Postman’s dynamic variables are powerful tools to help developers perform API testing with less effort. They provide flexible ways to inject and manage data directly into requests and scripts. They can be configured in the folder, environment, or even in a single request. They promote consistency and also by simplifying repetitive tasks they save a lot of time.
Table of Content
- Types of Dynamic Variables
- Step By Step Working with Dynamic Variables
- Examples of Postman Dynamic Variables
- Advanced Techniques and Tips
- Troubleshooting Dynamic Variables
- Conclusion