Skip to content

Instantly share code, notes, and snippets.

@dougtoppin
Last active June 20, 2021 19:03
Show Gist options
  • Save dougtoppin/92ad406e84c972dca98b5a9cc9abf143 to your computer and use it in GitHub Desktop.
Save dougtoppin/92ad406e84c972dca98b5a9cc9abf143 to your computer and use it in GitHub Desktop.
Postman is an exceptionally useful tool for testing ReST apis.
The following is an example of how to use the xml2Json method in a test if you are getting back xml and want to test elements in the response. The reason that I am posting this is that I could not get xml2Json to work until I discovered (using the new console in Version 4.7.2) that I needed the include the .$. in the object reference.
So that this is completely self contained all this is doing is setting an env var with an xml string, getting that
var, converting it to json and then evaluating the content.
postman.setEnvironmentVariable("testresponse", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><myResponse isError=\"false\"/>");
var xmlresponse = postman.getEnvironmentVariable("testresponse");
var jsonObject = xml2Json(xmlresponse);
tests["isError false"] = jsonObject.myResponse.$.isError === "false";
@ComputerKarate
Copy link

Thank you for posting this.
I have been trying all day to figure out why postman gives me an error on code that seems to work for everyone else.

@dougtoppin
Copy link
Author

Glad it was helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment