Skip to main content

Featured

How to Visualize your JSON 🕵🏼?

A simple way to view and segregate the complex JSON Hello Peeps, Welcome back to another blog 😉. I hope everything is going well for you. Many people prefer the JSON format for APIs, databases, and other applications for a variety of reasons. Some JSON requests or responses are simple and only contain a few objects. On numerous occasions, you may be required to handle a complex response. I wrote a blog about parsing complex responses before using https://jsonpathfinder.com/ In most cases, however, you will need to further investigate your JSON payload or responses for various reasons and analytical purposes. Are you a visual learner or a fan of mind maps? Then you will undoubtedly fall in love with this tool ❤️ JSON Crack I recently came across this tool and found it’s worth sharing with the community 👓 Once you land on the home page, click “GO TO EDITOR” Now you will see a sample JSON beatified and visualized. To visualize it, clear the left side pane and paste your JSON. For in...

How to handle SOAP API and parsing the response in Postman??

 SOAP is an XML-based protocol for accessing web services over HTTP. It has some specifications which could be used across all applications.

SOAP is known as the Simple Object Access Protocol, but in later times was just shortened to SOAP v1.2. SOAP is a protocol or in other words is a definition of how web services talk to each other or talk to client applications that invoke them. The diagram below shows the various building blocks of a SOAP Message.

The SOAP message is nothing but a mere XML document which has the below components.

  • An Envelope element that identifies the XML document as a SOAP message — This is the containing part of the SOAP message and is used to encapsulate all the details in the SOAP message. This is the root element in the SOAP message.
  • A Header element that contains header information — The header element can contain information such as authentication credentials which can be used by the calling application. It can also contain the definition of complex types which could be used in the SOAP message. By default, the SOAP message can contain parameters which could be of simple types such as strings and numbers, but can also be a complex object type.
  • A Body element that contains call and response information — This element is what contains the actual data which needs to be sent between the web service and the calling application. Below is an example of the SOAP body which actually works on the complex type defined in the header section. Here is the response of the Tutorial Name and Tutorial Description that is sent to the calling application which calls this web service.

Postman is not only for REST client, this supports SOAP APIs too. In the recent times Postman added many features to support the handling of SOAP APIs. Please read this blog to know more about directly importing the WSDL files into Postman.

Postman is not just for REST APIs. Now we can see an example of SOAP API and how to handle that in Postman. Parsing the SOAP response is a bit similar to REST, but there are some differences. We need to understand the SOAP response better to parse them.

For our demo, we will be using a Public SOAP API calculator endpoint for addition. Let’s get into our example.

Step 1: Create a new request in Postman and paste the URL: http://www.dneonline.com/calculator.asmx

Note: For all SOAP API endpoint the HTTP method will be POST.

Step 2: Along with the existing headers, make sure to add the “Content-Type” as “text/xml”

Step 3: We need to enter the body as raw XML

Paste the below content as your body:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">    <soapenv:Header/>    <soapenv:Body>       <tem:Add>          <tem:intA>5</tem:intA>          <tem:intB>12</tem:intB>       </tem:Add>    </soapenv:Body> </soapenv:Envelope>

Here the fields intA and intB are two different values which are editable. For every run we can update them and see different test results.

Step 4: Click “Save” and “Send”

Yes, we finally did a SOAP API call in Postman.

How do we parse the response?

We got the XML response back from the server which needs to be parsed/split as small elements to use that in future.

Method 1:

Step 1: Same as JSON response, we need to store the XML response as JSON object using xml2Json() method

var jsonObject = xml2Json(responseBody); 
console.log(jsonObject);

Now the response object looks like below:

Step 2: We are already aware of the variables in Postman, and we can now try to reach the desired element and store them as variables.

Ignore the first line in the response “<?xml version=”1.0" encoding=”utf-8"?>

Start from “soap:Envelope” tag

var result = jsonObject['soap:Envelope']['soap:Body']['AddResponse']['AddResult']; pm.environment.set("Result", result);

Now the result is stored in a variable which can be used further in our Test scripts/assertions.

That’s it. It’s simple.

Method 2:

Incase if you have few elements under the same tag you can save that path as a variable. And you can use that variable and append the name of the desired tag after that.

For example,

var respResult = jsonObject['soap:Envelope']['soap:Body']['AddResponse']; var result1 = respResult['AddResult']; pm.environment.set("Result1", result1);

Both of them returns the same value.

Method 3:

Using cheerio.js

If the response is lengthy and you need to go through many tag names to reach your desired element, we have another option too. One of my favorite things about Postman is, it’s abilities to support the JavaScript libraries. One such amazing library is cheerio.js in NPM package. Any XML/HTML page can be scraped using cheerio. We can use them in Postman for parsing the XML/HTML responses. Interesting isn’t? Let’s discuss more on this.

var cheerio = require('cheerio');
pm.test("Cheerio",() => {
const $ = cheerio.load(responseBody,{
ignoreWhitespace: true,
xmlMode: true
});
//Render XML
console.log("Entering logic");
console.log($.xml());
console.log($('AddResult').text());
pm.environment.set("resultFromCheerio", $('AddResult').text());
pm.test("Using Cheerio ", function ()
{
pm.expect($('AddResult').text()).to.eql("17");
});
});

By using this method we can directly access the tag name using the $ symbol. All the result fields will have the same value.

Wow, we actually parsed the XML response of an SOAP API. It might be a bit tough initially, so always try to understand the response structure. Once you are familiar you can try to adopt using cheerio because this will save lot of your time and can avoid lengthy scripts.

We discussed handling the SOAP API requests/responses in Postman. Each of them have their own way to parse the response. Hope you enjoyed reading this. We just tried with the single endpoint, try to use different SOAP APIs and play with the responses. This will help in writing the assertions.

You can have a look at my public workspace for getting to learn with some other SOAP API examples: https://www.postman.com/bpricilla/workspace/pricilla-s-api-space/collection/12539800-609bea35-3e2c-418d-b6b8-d3725c8450b4?ctx=documentation

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -

Originally published in https://synapse-qa.com/2021/08/04/apis-unleashed-04-soap-apis/

Comments

Post a Comment