0
0
Postmantesting~5 mins

Why chaining simulates real workflows in Postman

Choose your learning style9 modes available
Introduction

Chaining helps test how different parts of a system work together step-by-step, just like real users do.

When you want to test a login followed by accessing user data
When you need to create a resource first, then update or delete it
When testing a multi-step process like checkout in an online store
When verifying that data flows correctly between API calls
When simulating a user journey that depends on previous actions
Syntax
Postman
pm.sendRequest(request1, function (err, res1) {
    // Use data from res1
    pm.sendRequest(request2, function (err, res2) {
        // Continue chaining as needed
    });
});

Each request waits for the previous one to finish before starting.

You can pass data from one response to the next request.

Examples
This example logs in first, then uses the token to get the user profile.
Postman
pm.sendRequest({ url: 'https://api.example.com/login', method: 'POST', body: { mode: 'raw', raw: JSON.stringify({ username: 'user', password: 'pass' }) } }, function (err, res) {
    const token = res.json().token;
    pm.sendRequest({ url: 'https://api.example.com/profile', method: 'GET', header: { Authorization: `Bearer ${token}` } }, function (err, res2) {
        console.log(res2.json());
    });
});
This example creates an item, then deletes it using the returned ID.
Postman
pm.sendRequest({ url: 'https://api.example.com/items', method: 'POST', body: { mode: 'raw', raw: JSON.stringify({ name: 'item1' }) } }, function (err, res) {
    const itemId = res.json().id;
    pm.sendRequest({ url: `https://api.example.com/items/${itemId}`, method: 'DELETE' }, function (err, res2) {
        console.log('Item deleted');
    });
});
Sample Program

This script creates a post, then fetches it using the returned ID, simulating a real workflow.

Postman
pm.sendRequest({
    url: 'https://jsonplaceholder.typicode.com/posts',
    method: 'POST',
    header: { 'Content-Type': 'application/json' },
    body: { mode: 'raw', raw: JSON.stringify({ title: 'foo', body: 'bar', userId: 1 }) }
}, function (err, res) {
    if (err) {
        console.error('Error creating post:', err);
        return;
    }
    const postId = res.json().id;
    console.log('Post created with ID:', postId);

    pm.sendRequest({
        url: `https://jsonplaceholder.typicode.com/posts/${postId}`,
        method: 'GET'
    }, function (err2, res2) {
        if (err2) {
            console.error('Error fetching post:', err2);
            return;
        }
        console.log('Fetched post title:', res2.json().title);
    });
});
OutputSuccess
Important Notes

Chaining ensures tests follow the real order of actions users take.

It helps catch bugs that happen only when steps depend on each other.

Be careful with error handling to avoid silent failures in chains.

Summary

Chaining mimics real user workflows by linking requests step-by-step.

It passes data between requests to test connected actions.

This approach helps find issues in multi-step processes.