Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract a variable from XML using Postman?

Tags:

json

xml

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.

like image 208
Kyle Burriss Avatar asked Apr 23 '16 19:04

Kyle Burriss


People also ask

Can we convert XML to JSON in Postman?

Parse the XML to JSON using var jsonObject = xml2Json(responseBody); With Json Object You can parse as normal json.

Does postman work with XML?

As a part of Postman Tutorial – End to End , in this post, we will learn how can we send a XML payload and parameterized XML payload to request in Postman. We can pass payload to a request in multiple ways. Sending XML payload is one of them.


2 Answers

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.

like image 76
Kyle Burriss Avatar answered Oct 26 '22 15:10

Kyle Burriss


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);
like image 25
tom redfern Avatar answered Oct 26 '22 16:10

tom redfern