'I want to check the response body to check that it contains a particular user
I have a GET request which gives an array of user details.
I want to check that a particular username is there in the response, based on the environment variable set.
[
{
"natid":"1101$$$$",
"name":"User1",
"relief":"49000.00"
},
{
"natid":"1101$$$$",
"name":"User2",
"relief":"98000.00"
}
]
Solution 1:[1]
If you just wanted to check if the username is in the response array, you could use .some() which would return a true/false.
const resp = pm.response.json();
let findName = resp.some(obj => obj.name === `${pm.environment.get('username')}`);
pm.test("Check response contains username", () => {
pm.expect(findName, 'response does not contain username').to.be.true;
});
There are a number of different ways to check for a value in the response, researching and practising these will help in the future.
If you're raising questions on Stackoverflow, you need to attempt to solve the issue yourself before reaching out, if you need to seek help, show what you have tried and what's not working for you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Danny Dainton |
