What if your tests could always have the right data without you lifting a finger?
Why pre-request scripts prepare data in Postman - The Real Reasons
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you need to test an API that requires a fresh token or dynamic data every time you send a request. You open Postman and manually update the token or data before each request.
This manual updating is slow and easy to forget. You might send requests with expired tokens or wrong data, causing test failures and confusion. It wastes time and makes tests unreliable.
Pre-request scripts automatically prepare and set the needed data before each request runs. This means tokens, timestamps, or any dynamic info are always fresh and correct without manual work.
Set token manually in headers before each requestUse pre-request script to fetch and set token automaticallyIt enables fully automated, reliable API tests that always use the right data at the right time.
When testing a login API, a pre-request script can get a new access token automatically so you never send expired tokens.
Manual data updates are slow and error-prone.
Pre-request scripts automate data preparation before requests.
This makes API testing faster, more reliable, and less stressful.
Practice
Solution
Step 1: Understand the role of pre-request scripts
Pre-request scripts run before the request is sent to prepare or update any data needed for the request.Step 2: Differentiate from other script types
Scripts that check response status or display results run after the request, not before.Final Answer:
To prepare or update data needed for the request -> Option CQuick Check:
Pre-request scripts prepare data = C [OK]
- Confusing pre-request scripts with test scripts
- Thinking pre-request scripts check responses
- Assuming pre-request scripts run after requests
token in a pre-request script?Solution
Step 1: Identify the correct method to set a variable
In Postman scripts,pm.variables.setorpm.environment.setis used to set variables. Here,pm.variables.set('token', 'abc123');correctly sets the variable.Step 2: Understand other methods
pm.environment.getandpm.variables.getretrieve variables, not set them.pm.environment.deleteremoves variables.Final Answer:
pm.variables.set('token', 'abc123'); -> Option AQuick Check:
Set variable uses .set() method = B [OK]
- Using .get() instead of .set() to assign variables
- Confusing environment and variables methods
- Trying to delete variables when setting them
timestamp after running this pre-request script?const now = new Date().getTime();
pm.variables.set('timestamp', now);Solution
Step 1: Understand
This returns the current time in milliseconds since January 1, 1970 (Unix epoch) as a number.new Date().getTime()Step 2: Check variable assignment
The script sets the variabletimestampto this number usingpm.variables.set, which is correct syntax.Final Answer:
A number representing milliseconds since Jan 1, 1970 -> Option AQuick Check:
getTime() returns milliseconds number = D [OK]
- Thinking getTime() returns a date string
- Assuming variable is undefined
- Confusing set() method usage
pm.environment.set('authToken', getToken());
function getToken() {
return getToken();
}Solution
Step 1: Analyze the function call
The script callsgetToken()insidepm.environment.set('authToken', getToken()). The functiongetToken()returnsgetToken(), which calls itself again.Step 2: Identify recursion problem
This causes infinite recursion becausegetToken()calls itself directly, leading to a stack overflow.Final Answer:
The function getToken causes infinite recursion -> Option DQuick Check:
Function calls itself recursively = infinite recursion [OK]
- Thinking pm.environment.set is invalid
- Missing that getToken calls itself recursively
- Assuming variables can't be set in pre-request scripts
userId. Which pre-request script correctly achieves this?Solution
Step 1: Generate a random integer ID
Math.floor(Math.random() * 1000)generates a random whole number between 0 and 999, suitable for a user ID.Step 2: Correctly set the environment variable
pm.environment.set('userId', id);correctly stores the generated ID in the environment variableuserId.Final Answer:
const id = Math.floor(Math.random() * 1000); pm.environment.set('userId', id); -> Option BQuick Check:
Use Math.floor and .set() to store random ID = A [OK]
- Using .get() instead of .set() to store variables
- Not rounding random numbers to integers
- Deleting variables instead of setting them
