17
votes

I'm trying to extract a SessionId from the XML which is returned from a SOAP API.

I've read through the Postman documentation (several times over) but it wasn't the most helpful in achieving my goal.

What was suggested in a few blogs was to convert the XML to JSON, and then pick out the token and it's value from there, but that didn't help either.

I used the following in my Test:

var jsonObject = xml2Json(responseBody);
postman.setGlobalVariable("Session_Id", jsonObject.SessionID);

The above created the variable "Session_Id" but didn't actually assign a value to it. I'm stumped.

I'm definitely retrieving the data from the API, and it's viewable in Postman's "Body" Response.

3
I'm still seeking an answer to this, and I'm corroborating with the person who introduced me to the app. I'll send Postman an email if I don't come right in the next few days.Kyle Burriss
Whats the output for jsonObject and is jsonObject.SessionID a string. According to postman docs: postman.setGlobalVariable(variableName, variableValue): Sets a global variable "variableName", and assigns the string "variableValue" to it. Note: Only strings can be stored. Storing other types of data will result in unexpected behavior.Shaughn
I emailed Postman @Shaughn and one of their founders replied, advising me of the same thing. But he went on to say that the app automatically converts any other type to a string. Regardless, I found the issue. I wasn't drilling deep enough into the JSON blob. I ended up using: 'postman.setEnvironmentVariable("Session_Id", jsonObject.UserSessionToken.SessionId);' I made use of the Console.log and kept checking the output until I found what I needed.Kyle Burriss

3 Answers

31
votes

To extract a variable from XML using Postman, first convert your XML to JSON, using the xml2Json converter method:

var responseJson = xml2Json(responseBody);

(Where "responseBody" is your xml body.) Then use the console.log method to output your JSON data, as such:

console.log(responseJson);

Be sure to have followed this tutorial on Enabling Chrome Dev Tools in Postman

Inside your Test Runner, run the test, then right click and "Inspect" anywhere in the Runner. Select the "Console" tab once Chrome's Dev Tools launch. Expand the "Object" part.

Then drill-down (expand) until you see the Property whose data you need. Thereafter, set the variable by appending each drill-down level to the parameter you want:

postman.setGlobalVariable("Session_Id", responseJson.UserSessionToken.SessionID); 

In this case, "responseJson" is the object, "UserSessionToken" was the next level in the drill-down, and SessionId was the parameter I needed within that.

Note: This answer was the correct solution before v7.15.0 of postman. For versions later than this, the accepted answer may not work.

10
votes

Since Postman v7.15.0 the accepted answer did not work for me (it used to before the update). You have to put the path segments into square brackets.

For example, in the following XML response:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<QuotesPrivateResponse>
    <lease-service>
        <duration-in-months>24</duration-in-months>
    </lease-service>
</QuotesPrivateResponse>

to retrieve the value duration-in-months:

var response = xml2Json(responseBody);
var duration = response["QuotesPrivateResponse"]["lease-service"]["duration-in-months"];
pm.environment.set("duration", duration);
5
votes

Postman v7.20.1

I'd like to add my answer since above there are a couple of details that took me a while to solve:

  • how to cope with a multipart SOAP response
  • how to manage a JSON object
  • responseBody definition

Here's the first lines of my XML response to analyze:

------=_Part_694527_1470506002.1584708814081
Content-Type: application/xop+xml;charset=UTF-8;type="text/xml"
Content-Transfer-Encoding: 8bit
Content-ID: 
<e3bd82ac-d88f-49d4-8088-e07ff1c8d407>
    <?xml version="1.0" encoding="UTF-8" ?>
    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
        <env:Header/>
        <env:Body>
            <ns2:GenericResponse xmlns:ns2="http://XXXXXXXX">
                <ns2:Service IdcService="XXXXXXXX">
                    <ns2:Document>
                        <ns2:Field name="XXXXXXXX:isSetDefault">1</ns2:Field>
                        <ns2:Field name="XXXXXXXX">CHECKIN_UNIVERSAL</ns2:Field>

After I noticed it was a multipart I've ended up with this Postman Test:

var response = pm.response.text();
var responseBody = response.substr(response.indexOf('<env:')); 

pm.test("The body of the response is a valid XML", function () {
     pm.expect(xml2Json(responseBody)).to.exist;
});


pm.test("UCM file upload checkin succesfull", function(){

    var responseJson = xml2Json(responseBody);
    var JsonFields = (responseJson['env:Envelope']['env:Body']['ns2:GenericResponse']['ns2:Service']['ns2:Document']['ns2:Field']);

    JsonFields.forEach( field => {
    if (field.name == 'StatusMessage'){
        console.log("Field = " + field.name);
        pm.expect(field.text().to.include("Successfully checked in"));
        }
    }); 
});