Extract a value from XML response and set it as a

2020-07-22 17:18发布

I am trying to extract a value from a successful API request, which is sending a XML response, using Postman. Following is the way how I tried to catch the value that I need from the response.

var jsonObject = xml2Json(responseBody);
console.log(jsonObject);

postman.setGlobalVariable("Active_SAML", jsonObject.bankidCollectResponse.SAMLReferens); 
console.log(pm.globals.get("Active_SAML"));

That script written inside "Tests" tab and the console log out put is as below.

enter image description here

But when I execute the program, I get following error.

There was an error in evaluating the test script: TypeError: Cannot read property 'SAMLReferens' of undefined

I am not sure where I'm doing it wrong. Can anybody please point out me that?

标签: xml postman
1条回答
唯我独甜
2楼-- · 2020-07-22 18:06

Thanks to @ChathurangaChandrasekara comment, I did able to figure out the format that they expected.

// Convert XML output to JSON format
var jsonObject = xml2Json(responseBody);

// Since the converted JSON format is having Envelope and Body tags we need following format
var activeSamlVal = jsonObject['SOAP-ENV:Envelope']['SOAP-ENV:Body'].bankidCollectResponse.SAMLReferens;
console.log(activeSamlVal)

// Assigning the extracted value in to a global variable
pm.globals.set("SAML_key", activeSamlVal);
console.log(pm.globals.get("SAML_key"));
查看更多
登录 后发表回答