Compare 2 responses using Postman

Hi Team,

I have scenario where i need to compare 2 responses from Rest API with different test data.
Can i execute same Rest API in a loop in postman where it pickup test data 1 in 1st iteration and test data 2 in second iteration?

Post that I need to compare responses(response are in JSON format) for 2 iterations.


I think you can set a counter variable using for mark 2 iteration, use this counter, set 2 different variables to record 2 responses,then compare them.

I don’t know if it will help you, if not, I’m sorry,I‘’m a novice.

@SatejMirpagar - Have you tried using the collection runner in the postman app?
You can configure it with iterations count with which you want to run a collection / folder.

You can set a global / environment variable for the purpose of keeping the iteration count.
You can set the counter to 1 before starting the runner, as a global variable.

You can have the following simple test script for the request

let c = parseInt(pm.globals.get('counter'));
if (c == 1) {
    pm.test('should equal 1st iteration data', function() {
        // check here
} else if (c == 2) {
    pm.test('should equal 1st iteration data', function() {
        // check here
pm.globals.set(counter, c + 1);

pm.test('Check if the response is a JSON', function() {
    // check here

Hope this helps.

The approach above is valid and extendible to an arbitrary number of iterations, but to compare only two responses, I would go with a simpler, more explicit approach:

  1. Make two requests in Postman, lets call them A and B
  2. As part of the post-request/tests script of request A, register the response as a global variable using the following snippet
pm.globals.set("aResponseAsJson", pm.response.json());
  1. As part of the post-request/tests script of request B, you now have access to response A in a global var and the response from the current request. You can compare them as needed using ChaiJS BDD style. Finish by cleaning up the global variable.
pm.test("compare responses", () =>

Thanks mates - the information really helped!

Would you want to use to.deep.equal() here incase the response has nested properties?


I’ve been struggling with this issue in the past week and wrote the following test script:

const responseKey = [, 'response'].join('/');

let res = '';

try {
    res = JSON.stringify(pm.response.json());
} catch(e) {
    res = pm.response.text();

if (!pm.globals.has(responseKey)) {
    pm.globals.set(responseKey, res);
} else {
    // this tests the actual response with one saved in the global for the same request
    pm.test(responseKey, function () {
        const response = pm.globals.get(responseKey);
        try {
            const data = pm.response.json();
        } catch(e) {
            const data = pm.response.text();

You this code in a collection or single request.

Hope it helps.