Introduction to Variables and Scripting #
This section expands on using variables in Postman and introduces scripting for dynamic request behavior.
Variables in Postman (Continued) #
Variables store reusable values, promoting DRY (Don’t Repeat Yourself) principles and protecting sensitive data like API keys.
Variable Scopes #
Postman variables have different scopes, affecting their accessibility:
- Global: Accessible in all workspaces.
- Collection: Accessible within a collection.
- Environment: Accessible within an environment.
- Data: Used with data files for running multiple iterations of a request.
- Local: Specific to a single request or script.
Postman resolves variable values based on the narrowest available scope. This section focuses on collection variables.
Scripting in Postman #
Postman scripts add dynamic behavior to collections. Scripts execute during two events:
- Pre-request Script: Runs before a request is sent.
- Post-request Script: Runs after a response is received.
This section focuses on post-request scripts.
The pm
Object
#
The pm
object provides access to Postman data, including the response body, variables, and utilities.
pm.response.json()
: Accesses the JSON response body.pm.collectionVariables.get("variableName")
: Retrieves a collection variable’s value.pm.collectionVariables.set("variableName", "variableValue")
: Sets a collection variable.
Your First Script #
This task introduces a simple post-request script.
- In the “add book” request, modify the book data in the “Body” tab.
- In the “Tests” tab, add the following JavaScript code:
console.log(pm.response.json());
- Save and send the request.
The response data will be logged to the Postman Console.
Capturing the New Book ID #
This task demonstrates setting a variable using a post-request script.
Setting and Getting Collection Variables #
pm.collectionVariables.set("variableName", value)
: Sets a collection variable.pm.collectionVariables.get("variableName")
: Retrieves a collection variable.
Local Variables #
JavaScript const
(for constant values) or let
(for variable values) keywords can be used to declare local variables within scripts.
Setting the New Book ID as a Variable #
- In the “add book” request, modify the book data.
- In the “Tests” tab, replace the previous script with:
// Save the ID from the response
const id = pm.response.json().id;
// Store the ID as a collection variable
pm.collectionVariables.set("id", id);
- Save and send the request.
The book’s ID from the response is now stored in the id
collection variable. This can be verified in the collection’s “Variables” tab. This {{id}}
variable can now be used in other requests within the collection.