Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract a value from XML response and set it as a global variable in Postman

Tags:

xml

postman

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?

like image 820
AnujAroshA Avatar asked Mar 29 '18 07:03

AnujAroshA


1 Answers

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"));
like image 117
AnujAroshA Avatar answered Nov 14 '22 05:11

AnujAroshA