Bird
Raised Fist0
Postmantesting~15 mins

Chaining request data in Postman - Build an Automation Script

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Chain two API requests using Postman
Preconditions (2)
Step 1: Send a POST request to /createUser with JSON body {"name": "John", "job": "Developer"}
Step 2: Capture the 'id' value from the response JSON
Step 3: Send a GET request to /getUserDetails using the captured 'id' as a path parameter
Step 4: Verify the GET response contains the same 'name' and 'job' as sent in the POST request
✅ Expected Result: The GET request returns user details matching the data sent in the POST request, confirming the chaining of request data works correctly.
Automation Requirements - Postman Tests (JavaScript)
Assertions Needed:
Verify POST response status is 201
Verify POST response contains an 'id' field
Verify GET response status is 200
Verify GET response 'name' equals 'John'
Verify GET response 'job' equals 'Developer'
Best Practices:
Use pm.environment variables to store and reuse data between requests
Use pm.test for assertions with clear messages
Use JSON.parse to handle response bodies
Avoid hardcoding values; use variables for flexibility
Automated Solution
Postman
/* POST /createUser request Tests tab script */
pm.test('POST status is 201', () => {
    pm.response.to.have.status(201);
});

const responseJson = pm.response.json();
pm.test('Response has id', () => {
    pm.expect(responseJson).to.have.property('id');
});

// Save id to environment variable for next request
pm.environment.set('userId', responseJson.id);

/* GET /getUserDetails request Pre-request Script */
// Use the saved userId in the request URL
const userId = pm.environment.get('userId');
pm.variables.set('userId', userId);

/* GET /getUserDetails request Tests tab script */
pm.test('GET status is 200', () => {
    pm.response.to.have.status(200);
});

const getResponse = pm.response.json();
pm.test('Name is John', () => {
    pm.expect(getResponse.name).to.eql('John');
});

pm.test('Job is Developer', () => {
    pm.expect(getResponse.job).to.eql('Developer');
});

The POST request test script first checks the response status is 201, which means the user was created successfully. It then parses the JSON response and asserts that an 'id' field exists. This 'id' is saved to an environment variable userId for use in the next request.

In the GET request, the pre-request script retrieves the saved userId and sets it as a variable to be used in the request URL path parameter.

The GET request test script verifies the response status is 200, meaning the user details were fetched successfully. It then asserts that the returned 'name' and 'job' fields match the values sent in the POST request, confirming the chaining of data between requests.

This approach uses Postman's environment variables to pass data between requests, and pm.test with clear messages to make debugging easier.

Common Mistakes - 3 Pitfalls
{'mistake': "Not saving the 'id' from the POST response to an environment variable", 'why_bad': "Without saving the 'id', the GET request cannot use it to fetch the correct user details, breaking the chain.", 'correct_approach': "Use pm.environment.set('userId', responseJson.id) to save the 'id' after the POST request."}
{'mistake': 'Hardcoding the userId in the GET request URL instead of using variables', 'why_bad': "Hardcoding makes the test inflexible and fails if the 'id' changes or is dynamic.", 'correct_approach': 'Use a variable like {{userId}} in the GET request URL and set it dynamically in scripts.'}
Not checking response status codes before accessing response data
Bonus Challenge

Now add data-driven testing with 3 different user data sets for the POST request and verify each GET response accordingly.

Show Hint

Practice

(1/5)
1. What is the main purpose of chaining request data in Postman?
easy
A. To pass data from one API request to another for connected testing
B. To run multiple requests at the same time
C. To change the request method automatically
D. To generate random data for each request

Solution

  1. Step 1: Understand chaining request data

    Chaining request data means using data from one API response in the next request to keep tests connected.
  2. Step 2: Identify the purpose

    This helps simulate real user flows where one action depends on the previous one.
  3. Final Answer:

    To pass data from one API request to another for connected testing -> Option A
  4. Quick Check:

    Chaining = passing data between requests [OK]
Hint: Chaining means passing data forward between requests [OK]
Common Mistakes:
  • Thinking chaining runs requests simultaneously
  • Confusing chaining with changing HTTP methods
  • Assuming chaining generates random data
2. Which Postman script correctly saves a value from a JSON response to an environment variable named userId?
easy
A. pm.response.set('userId', pm.environment.json().id);
B. pm.environment.get('userId', pm.response.json().id);
C. pm.response.get('userId', pm.environment.json().id);
D. pm.environment.set('userId', pm.response.json().id);

Solution

  1. Step 1: Identify correct method to save variable

    Use pm.environment.set(key, value) to save data to environment variables.
  2. Step 2: Extract value from response JSON

    Use pm.response.json() to parse JSON and get the id field.
  3. Final Answer:

    pm.environment.set('userId', pm.response.json().id); -> Option D
  4. Quick Check:

    Set environment variable = pm.environment.set [OK]
Hint: Use pm.environment.set to save data from pm.response.json() [OK]
Common Mistakes:
  • Using pm.environment.get to set a variable
  • Confusing pm.response and pm.environment methods
  • Trying to get data from environment instead of response
3. Given this Postman test script after a request:
const jsonData = pm.response.json();
pm.environment.set('token', jsonData.auth.token);

What will be the value of the environment variable token if the response JSON is {"auth": {"token": "abc123"}}?
medium
A. null
B. undefined
C. "abc123"
D. Error: token not found

Solution

  1. Step 1: Parse the response JSON

    The response JSON has an object with auth containing token with value "abc123".
  2. Step 2: Set environment variable

    The script sets token variable to jsonData.auth.token, which is "abc123".
  3. Final Answer:

    "abc123" -> Option C
  4. Quick Check:

    token value = "abc123" [OK]
Hint: Check JSON path matches response structure exactly [OK]
Common Mistakes:
  • Assuming token is undefined if nested
  • Forgetting to parse JSON before accessing
  • Expecting error when token exists
4. You wrote this script to chain data:
const data = pm.response.json();
pm.environment.set('userId', data.user.id);

But the environment variable userId is always empty after the request. What is the likely problem?
medium
A. The script must run before the request
B. The response JSON does not have a user object
C. You must use pm.variables.set instead
D. pm.environment.set cannot save variables

Solution

  1. Step 1: Check JSON structure

    If data.user is undefined, accessing data.user.id returns undefined, so variable is empty.
  2. Step 2: Confirm environment.set works correctly

    pm.environment.set works if given a valid value, so problem is likely missing user in response.
  3. Final Answer:

    The response JSON does not have a user object -> Option B
  4. Quick Check:

    Missing JSON key = empty variable [OK]
Hint: Verify JSON keys exist before accessing nested values [OK]
Common Mistakes:
  • Assuming pm.environment.set is broken
  • Using wrong method like pm.variables.set
  • Running script before response is received
5. You want to chain two requests where the first returns a list of users:
{"users": [{"id": 1}, {"id": 2}]}

You want to save the id of the first user to an environment variable firstUserId and use it in the second request URL as /users/{{firstUserId}}. Which script correctly does this in the first request's Tests tab?
hard
A. const jsonData = pm.response.json(); pm.environment.set('firstUserId', jsonData.users[0].id);
B. const jsonData = pm.response.json(); pm.environment.set('firstUserId', jsonData.users.id[0]);
C. const jsonData = pm.response.json(); pm.environment.set('firstUserId', jsonData.users[1].id);
D. const jsonData = pm.response.json(); pm.environment.set('firstUserId', jsonData.user[0].id);

Solution

  1. Step 1: Parse response JSON correctly

    The response has a users array with objects containing id. The first user is at index 0.
  2. Step 2: Save first user's id properly

    Access jsonData.users[0].id and save it with pm.environment.set.
  3. Final Answer:

    const jsonData = pm.response.json(); pm.environment.set('firstUserId', jsonData.users[0].id); -> Option A
  4. Quick Check:

    Array index 0 for first user id [OK]
Hint: Use array index [0] to get first item in JSON array [OK]
Common Mistakes:
  • Using wrong array index or property name
  • Accessing id as users.id[0] instead of users[0].id
  • Using singular 'user' instead of 'users'