Postman - Storing and Retrieving Numeric Values from Environment Variables
If you are using Postman for your API testing and if you want to store and retrieve numeric values from environment variables, then here is a simple trick - use "Number" function. Let me explain the problem and solution.
Problem: I want to store numeric value in environment variable, for example - response time, then I want to retrieve the value from this environment variable and use for comparison.
Solution:
- To store response time in environment variable, use setEnvironmentVariable function
- To retrieve the value from environment variable, use postman.
<variable\_name> - To convert variable value to number for comparison, use Number() function
# Store response time in environment variable:
postman.setEnvironmentVariable("response_time", responseTime);
# Retrieve response time from environment variable:
var respTime = environment.response_time;
# Convert response time to numeric value and compare:
if (Number(respTime) > 1000) {
// Some Logic
}
# OR
if (Number(environment.response_time) > 1000) {
// Some Logic
}