userId after running this test if the response body is {"id": 42, "name": "Alice"}?pm.test("Save userId", function () { var jsonData = pm.response.json(); pm.environment.set("userId", jsonData.id); });
Postman environment variables always store values as strings. Even though jsonData.id is a number (42), when saved using pm.environment.set, it is converted to the string "42".
token to an environment variable authToken. Which assertion correctly checks that authToken is not empty in a test script?To get an environment variable, use pm.environment.get('variableName'). To check it is not empty, use to.not.be.empty. Option A correctly does this.
response.data.id to an environment variable but it does not work. What is the likely cause?pm.test("Save ID", function () { var id = pm.response.data.id; pm.environment.set("userId", id); });
Postman response object does not have a data property directly. To access JSON response data, you must parse it using pm.response.json(). So pm.response.data is undefined, causing the failure.
Setting environment or collection variables in test scripts allows saving response data that can be used in later requests during a collection run. This is the standard way to pass data between requests.
Postman environment variables have size limits (usually a few KB). Saving very large response bodies can lead to truncation or errors. They do not compress or convert data automatically.