Pre-request Scripts
A Pre-request Script is a JavaScript snippet that runs before an API request is sent.
It is commonly used to prepare everything required before the request executes, such as:
- Setting authentication tokens.
- Creating or updating environment variables.
- Generating timestamps or random values.
- Fetching data from another API before sending the request.
Where Can You Add a Pre-request Script?
A pre-request script can be added at different levels in Postman.
Request Level
Runs only before the selected request.
Collection Level
Runs before every request in the collection.
Folder Level
Runs before all requests inside a specific folder.
Interview Tip:
"We used collection-level pre-request scripts to automatically retrieve and set authentication tokens before every API request, eliminating repetitive scripting and ensuring all requests used a valid token."
Test Scripts
A Test Script is a JavaScript function that runs after an API response is received.
It is mainly used to validate API responses by writing assertions.
Common Uses
- Validate HTTP status codes.
- Verify response body data.
- Check response headers.
- Measure response time.
- Ensure the API behaves as expected.
Where Can You Add Test Scripts?
Test scripts can be added at:
- Request Level
- Collection Level
- Folder Level
They execute after the corresponding API response is received.
Interview Tip:
"We used Postman test scripts to verify that the Login API returned a 200 OK status, generated the correct authentication token, and responded within 300 ms, ensuring API reliability."
Writing Assertions with JavaScript (Chai)
Postman uses the Chai Assertion Library for writing test assertions.
Common assertion methods include:
pm.test()pm.expect()pm.response.to.have
Assertions can validate:
- Status codes
- Response body
- Headers
- Response time
Example: Validate Status Code
pm.test("Status code is 201", function () {
pm.response.to.have.status(201);
});
Example: Validate Response Time
pm.test("Response time is below 500ms", function () {
pm.expect(pm.response.responseTime).to.be.below(500);
});
These assertions help ensure that the API behaves correctly after every request.
Extracting Values from Responses
Postman provides built-in methods for extracting values from API responses.
Extract JSON Response
pm.response.json();
Used to parse JSON responses and access specific fields.
Extract Plain Text Response
pm.response.text();
Used when the response is plain text instead of JSON.
Extract Header Values
pm.response.headers.get("header-name");
Used to retrieve the value of a specific response header.
Interview Tip:
"We usedpm.response.json()to extract authentication tokens from login responses,pm.response.headers.get()to validate response headers, andpm.response.text()when working with plain-text responses."
Passing Data Between Requests (Request Chaining)
Request chaining allows one request to use data returned by a previous request.
The most common approach is:
- Extract a value from the response.
- Store it in a variable.
- Use that variable in the next request.
Example
Store the User ID from the response.
pm.environment.set("userId", pm.response.json().user.id);
Use the variable in the next request.
{{userId}}
This technique is commonly used for:
- Authentication tokens
- User IDs
- Session IDs
- Order IDs
- Transaction IDs
Interview Tip:
"After a successful login, we extracted the authentication token from the response and stored it in an environment variable so that all subsequent API requests could use the same token."
Postman Monitors
Postman Monitors allow you to schedule automated executions of a Postman collection at predefined intervals.
They continuously monitor:
- API availability
- API health
- Response time
- Performance
- Reliability
Monitors help detect issues in deployed APIs without requiring manual execution.
Interview Tip:
"We used Postman Monitors to run API collections at scheduled intervals, ensuring production APIs remained available and performed within acceptable response times."
FAQs
What is a Pre-request Script?
A Pre-request Script is JavaScript code that runs before an API request. It is commonly used to set authentication tokens, initialize variables, or fetch data required for the request.
Where can you add a Pre-request Script?
You can add a Pre-request Script at:
- Request level
- Folder level
- Collection level
What is a Test Script?
A Test Script is JavaScript code that runs after an API response is received. It validates the response using assertions.
What can you validate using Test Scripts?
Test Scripts can validate:
- HTTP status codes
- Response body
- Response headers
- Response time
- Business logic
How do you write assertions in Postman?
Assertions are written in the Tests tab using JavaScript and the Chai Assertion Library with methods such as pm.test(), pm.expect(), and pm.response.to.have.
How do you extract values from an API response?
Use:
pm.response.json()for JSON responses.pm.response.text()for text responses.pm.response.headers.get()for response headers.
How do you pass data between API requests?
Extract the required value from one response using a Test Script, store it using pm.environment.set(), and reference it in the next request using:
{{variableName}}
What are Postman Monitors?
Postman Monitors are scheduled, automated executions of Postman collections that continuously monitor API health, availability, performance, and response times.