5
votes

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?

1
Anuja, I am not a postman guru but as of your description you are passing the entire soap response to xml2Json method. And in your console log you can see that the envelope is still there. However when you are accessing the properties you simply assume you map only the body part of the message but not the soap modifiers such as envelope. I think you have to "address" using the full hierarchy. Ref : github.com/postmanlabs/postman-app-support/issues/912Chathuranga Chandrasekara

1 Answers

8
votes

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"));