Kyle Burriss
Kyle Burriss

Reputation: 751

How do I extract a variable from XML using Postman?

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.

Upvotes: 23

Views: 69417

Answers (3)

tom redfern
tom redfern

Reputation: 31770

The accepted answer did not work for me (it used to before the update to v7.15.0). If your field names have certain characters in them then 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);

This is necessary when any of the element names in your xml doc contain hyphens(-), underscores(_), or colons(:), and there may be other characters which cause this behavior.

Upvotes: 17

Kyle Burriss
Kyle Burriss

Reputation: 751

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.

Upvotes: 37

tuxErrante
tuxErrante

Reputation: 1372

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

Upvotes: 9

Related Questions