Bhaumik
(Bhaumik P)
July 30, 2020, 1:09pm
1
I am new to postman
My Response Body is like something i want to check first name is match or not ?
can anyone help ?
{
"status": true,
"status_code": 200,
"data": {
"first_name": {
"result": "Match"
},
"middle_name": {
"result": "No Match"
},
"last_name": {
"result": "Match"
},
"dob": {
"result": "No Match"
},
"city": {
"result": "Match"
},
"zip": {
"result": "No Match"
},
"state": {
"result": "No Match"
},
"address": {
"result": "Match"
},
"gender": {
"result": "No Match"
},
"phone": {
"result": "No Match"
},
"email": {
"result": "No Match"
}
},
"transaction_id": "15961139575f22c42541d3ahp2Qw",
"request_id": "00001"
}
1 Like
Welcome @Bhaumik !
In the tests tab of your request, you’re going to want to add the following code:
pm.test("First name matches", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.data.first_name).have.property('result', 'Match');
});
All you’re doing here is just checking if a json value equals something, so it’s a pretty easy test to write.
You can also use the little test script snippets on the right hand side of the tests panel to get you started for things like this in the future.
2 Likes
Bhaumik
(Bhaumik P)
July 31, 2020, 6:09am
3
1 Like
dyom
(David Day)
August 5, 2020, 6:21pm
4
Can we do something similar with an XML response? My use case uses XML rather than JSON.
I would like to capture my sessionid value when the STATUS is 200.
200
You could probably use the same script @odanylewycz mentioned in this post:
Hi @nguyenvietdungqsq
Welcome to the community!
In addition you can use cheero.js to load the HTML, and parse your results.
const response = cheerio.load(pm.response.text(), {
ignoreWhitespace: true,
xmlMode: true
});
const sessionId = response('script').find('sessionId')
The above may not working exactly, as I am shooting from the hip, but you can use cheerio to parse through your html and get to your element and then parse out the js from there.
However, I will have to agr…
2 Likes
odanylewycz
(Orest Danylewycz)
August 6, 2020, 12:05pm
6
Hi @dyom ,
Welcome to the community!
You certainly can do XML parsing in postman. As @allenheltondev referred to my post, you can use cheerio.js.
I made a video that covers it pretty well. It also links to the code you can copy and use for your use case. Take a look here:
Hope it helps!
1 Like