Compare responses from two POST requests

Let us say, there are two APIs which accept POST requests. Both the APIs perform the same job(one is an old API and another is the new API, there has been some migration of code and hence the API is also getting renewed).

As regression, I am required to send POST requests to both the APIs then compare the responses(the entire response body except few fields like ID, Timestamp etc). There SHOULD NOT be any difference. If there is a difference then it is a bug.

Here is the catch:
The comparison for the entire JSON response has to be done 100+ times with different bodies in the POST request. (I am not sure if getting the data from a csv file is a good option for 100+ data).

Looking forward to hearing from you on how you would like to proceed on this problem of comparing two JSON response bodies in POSTMAN and repeat for 100+ times EXCLUDING one or more fields like IDs, Timestamp which will never match.

1 Like

postman support lodash so its as simple as:

console.log(_.isEqual(oldresponse, newresponse, (value1, value2, key) => {

    return key === "key1" || key === "key2" ? true : undefined;

}))

were key1,key2 etc are keys you don’t want to compare.

  1. so create two requests,

  2. in first request (old endpoint) store value to an environment varaible say ‘oldresponse’

  3. in second request ( new endpoint) store value to ‘newresponse’

  4. And in test script session of second request just use

console.log(_.isEqual(pm.environment.get('oldresponse'),pm.environment.get('newresponse'), (value1, value2, key) => {
        //this will ignore comparing the value if key is timestamp or id
        return key === "timestamp" || key === "id" ? true : undefined;

    }))

Thank you. So basically, if there is a mismatch then it returns False, if there is no mismatch then it returns True.

Now how to find out what didn’t match?

A way you could brute force these tests to figure out which values were different:

const ignoreProperties = ['id', 'timestamp'];
const newResponse = pm.response.json();
const oldResponse = JSON.parse(pm.environment.get('oldResponse');
const oldResponseKeys = Object.keys(oldResponse);
_.foreach(oldResponseKeys, function(key) {
  if(!ignoreProperties.includes(key)){
    pm.test(`New response has same value for `${key}` property`, function(){
      pm.expect(newResponse).to.have.property(key, oldResponse[key]);
    });
  }
});

This iterates over every property in the old response and makes sure the new response has the property and that the values match.

2 Likes
console.log(_.isEqual(first, second, (value1, value2, key) => {

    let result=  key === "mean" || key === "mean" ? true : undefined;

    result || JSON.stringify(value1) === JSON.stringify(value2) ? undefined : console.log(value1, value2,  JSON.stringify(value1) === JSON.stringify(value2)  );

    return result

}))

This will print the difference also

  • {referenceNumber: “ADR-2021-100000201548”, statusCode: 2000, statusMessage: “File successfully uploaded”}

  • :arrow_forward:{statusCode: 2000, statusMessage: “File successfully uploaded”, referenceNumber: “ADR-2021-1000000301922”}

false

true

I have used the above script. At the end of the second POST response, it says false but at the end it says true. True is correct but not sure why it prints false as well.

remove the console.log inside the function , it is there to print each comparison . it will first stringify two object and validate if its equal if its not equal then it does a deep comparizon of values instead. I just added it for demo purpose

you want only the final result.

console.log(_.isEqual(first, second, (value1, value2, key) => {

    let result=  key === "mean" || key === "mean" ? true : undefined;

    return result

}))

Understood, I actually want only the differences between the two. With the above I need to manually check where the difference is because it returns both same and difference.

Did you try the script I recommended?

I get the below error - JSONError: Unexpected token ‘o’ at 1:2
[object Object]

Make sure to do JSON.stringify on your response before saving it to an environment variable.

pm.environment.set('oldResponse', JSON.stringify(pm.response.json()));
1 Like

That might not work in case of nested properties

No it wouldn’t, but it’s easy enough to add a recursive function in there to navigate through all the nested objects.

I didn’t see mention of nested objects in the post, so the thought hadn’t crossed my mind.

1 Like

Thank you for your post. Can you please help me to iterate nested objects for comparing postman request.

Sample JSON
{
“value”: [
{
“DocumentId”: “”,
“DocumentClass”: “”,
“MetaData”: [
{
“PropertyName”: “”,
“Type”: “”,
“Value”: “”
},
{
“PropertyName”: “”,
“Type”: “”
“Value”: “”
}
],
}