API versioning validation helps make sure your app talks to the right version of an API. This avoids errors when the API changes.
API versioning validation in Postman
pm.test("API version is correct", function () { const responseJson = pm.response.json(); pm.expect(responseJson.version).to.eql("v1.0"); });
Use pm.response.json() to parse the API response body as JSON.
Use pm.expect() with .to.eql() to check exact equality.
pm.test("API version is v2", function () { const version = pm.response.headers.get('API-Version'); pm.expect(version).to.eql('v2'); });
pm.test("API version starts with v1", function () {
const responseJson = pm.response.json();
pm.expect(responseJson.version).to.match(/^v1/);
});This Postman test script checks that the API response JSON has a version field exactly equal to 'v1.0'.
pm.test("Validate API version in response body", function () { const responseJson = pm.response.json(); pm.expect(responseJson.version).to.eql("v1.0"); });
Always confirm where the API version is returned: in headers or response body.
Use exact matches to avoid confusion between similar versions like 'v1' and 'v10'.
Keep your tests updated when the API versions change or new versions are added.
API versioning validation ensures your app uses the correct API version.
Check version from response body or headers depending on API design.
Use Postman tests with pm.expect() to verify version values.
Practice
Solution
Step 1: Understand API versioning validation
API versioning validation checks if the API response matches the expected version.Step 2: Identify the main goal in Postman
In Postman, this means verifying the version value in the response body or headers.Final Answer:
To ensure the API response uses the correct version -> Option DQuick Check:
API version check = Correct version [OK]
- Confusing version check with response time
- Checking URL reachability instead of version
- Testing authentication instead of version
Solution
Step 1: Check correct method to get header value
Use pm.response.headers.get('header-name') to get header value in Postman.Step 2: Use pm.expect() with .to.eql() for assertion
pm.expect(...).to.eql('v2') correctly asserts the value equals 'v2'.Final Answer:
pm.expect(pm.response.headers.get('api-version')).to.eql('v2'); -> Option BQuick Check:
Use headers.get() + pm.expect() [OK]
- Using pm.response.headers['api-version'] which is undefined
- Calling to.eql() directly on header getter without pm.expect()
- Checking version in body when question asks for header
pm.test('Check API version', () => {
pm.expect(pm.response.json().version).to.equal('2.0');
});Solution
Step 1: Understand the test assertion
The test expects the version value to be exactly '2.0'.Step 2: Compare actual response version with expected
The response has version '1.0', which does not equal '2.0', so assertion fails.Final Answer:
Test fails because version is '1.0', not '2.0' -> Option CQuick Check:
Expected '2.0' vs actual '1.0' = Fail [OK]
- Assuming test passes if key exists
- Thinking .to.equal() ignores value differences
- Confusing syntax error with assertion failure
pm.test('API version is v1', () => {
pm.expect(pm.response.headers['api-version']).to.eql('v1');
});
But the test always fails. What is the likely issue?Solution
Step 1: Understand how to access headers in Postman
pm.response.headers is a special object; headers must be accessed with .get('header-name').Step 2: Identify the error in header access
Using pm.response.headers['api-version'] returns undefined, causing assertion failure.Final Answer:
pm.response.headers is not an object with keys, use .get() method instead -> Option AQuick Check:
Use headers.get() to access headers [OK]
- Accessing headers like a normal object
- Changing header name case unnecessarily
- Confusing .to.eql() with .to.equal() causing failure
Solution
Step 1: Understand how to check multiple allowed values
pm.expect(...).to.be.oneOf(array) checks if value is in the array.Step 2: Analyze each option's correctness
pm.test('API version is valid', () => { const version = pm.response.headers.get('api-version'); pm.expect(version).to.be.oneOf(['v1', 'v2', 'v3']); }); uses .to.be.oneOf(['v1','v2','v3']) correctly. pm.test('API version is valid', () => { const version = pm.response.headers.get('api-version'); pm.expect(version).to.include(['v1', 'v2', 'v3']); }); uses .to.include(array) which is invalid. pm.test('API version is valid', () => { const version = pm.response.headers.get('api-version'); pm.expect(version === 'v1' || 'v2' || 'v3').to.be.true; }); uses incorrect boolean logic. pm.test('API version is valid', () => { const version = pm.response.headers.get('api-version'); pm.expect(version).to.eql('v1' || 'v2' || 'v3'); }); uses .to.eql() with || which evaluates incorrectly.Final Answer:
pm.expect(version).to.be.oneOf(['v1', 'v2', 'v3']); -> Option AQuick Check:
Use .to.be.oneOf() for multiple allowed values [OK]
- Using .to.include() incorrectly on array
- Writing boolean OR logic inside pm.expect()
- Using .to.eql() with OR operator causing wrong check
Ready to prove it?
Pass the test and this pattern moves unknown
