Categories
Bizagi Tips and Tricks

How to Use Postman to call Bizagi web services

Introduction

Postman is a powerful API testing tool that allows developers and testers to send requests, validate responses, and automate workflows. While Postman works seamlessly with JSON APIs, working with XML responses often requires additional processing.

In this article, we will:

  • Configure Postman to make web service calls.
  • Explain why XML transformation is necessary.
  • Use JavaScript in Postman’s “Tests” tab to modify XML responses dynamically.

By the end, you’ll be able to restructure API responses in real-time, making API testing more efficient.

Configuring Postman to Make Web Service Calls

In this section, I will show how to configure Postman to call the Bizagi Web Service EntityManagerSOA for the operation getEntitiesAsString. This is an HTTP POST call that allows retrieving entity records from Bizagi in XML format.

Step 1: Create a new environment, call it Dev and add the following variables:

hostnamehttps://dev____.bizagi.com/ Replace this with your app URL
entitypStatus – this is the name of the Bizagi ent. Replace this with your entity.
operationPathwebservices/EntityManagerSOA.asmx/getEntitiesAsString

Step 2: Create a New Request. Call it getEntitiesAsString and set the request type to POST. Add the following Headers:

Content-Typeapplication/x-www-form-urlencoded
Content-Lengthlength

Step 3: Provide the Request Body. Select raw, Text

entitiesInfo=<BizAgiWSParam><EntityData><EntityName>{{entity}}</EntityName></EntityData></BizAgiWSParam>

Step 4: Click “Send” to make the API call. What do you notice? The answer is a string containing an encoded version of the XML. So this takes us to the next phase: Convert String to XML and apply necessary transformation.

Why XML Transformations are Necessary

APIs sometimes return data in formats that are not immediately useful. Here are a few reasons why XML transformation is essential:

  • Unwanted elements: The API returns unnecessary data that should be removed.
  • Incorrect structure: The response doesn’t match the expected schema.
  • Missing attributes: Values need to be moved from nested elements into attributes.

Example: From Raw API Response

… to XML

<string xmlns="http://tempuri.org/">
	<?xml version="1.0" encoding="utf-8"?>
	<BizAgiWSResponse>
		<Entities>
			<pStatus key="1">
				<sCode>001</sCode>
				<sName>Active Now</sName>
				<kStatusType entityName="pStatusType" key="1">
					<sCode>active</sCode>
					<sName>Active</sName>
				</kStatusType>
			</pStatus>
			<pStatus key="2">
				<sCode>002</sCode>
				<sName>Pending...</sName>
				<kStatusType entityName="pStatusType" key="2">
					<sCode>pending</sCode>
					<sName>Pending</sName>
				</kStatusType>
			</pStatus>
			<pStatus key="3">
				<sCode>003</sCode>
				<sName>Denied</sName>
				<kStatusType entityName="pStatusType" key="3">
					<sCode>dinied</sCode>
					<sName>Dinied</sName>
				</kStatusType>
			</pStatus>
		</Entities>
	</BizAgiWSResponse>
</string>

… to a Transformed XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<BizAgiWSResponse>
  <Entities>
    <pStatus key="1">
      <sCode>001</sCode>
      <kpStatusType_sCode>asdasdt</kpStatusType_sCode>
      <kpStatusType_sName>asdasd</kpStatusType_sName>
    </pStatus>
    <pStatus key="2">
      <sCode>002</sCode>
      <kpStatusType_sCode>4rfsd</kpStatusType_sCode>
      <kpStatusType_sName>sdfsadf</kpStatusType_sName>
    </pStatus>
    <pStatus key="3">
      <sCode>003</sCode>
      <kpStatusType_sCode>23dewd</kpStatusType_sCode>
      <kpStatusType_sName>wer234</kpStatusType_sName>
    </pStatus>
  </Entities>
</BizAgiWSResponse>

Here, we have:

  • Removed <sName>.
  • Deleted <kpStatusType> but preserved its values.
  • Repositioned <sCode> and <sName> as independent elements.

Postman allows us to modify the response dynamically using JavaScript.

Using JavaScript in Postman’s “Tests” Tab for XML Transformation

Go to Scripts > Post-response

var xml2js = require('xml2js');

pm.test("Extract and Clean XML", function () {
    var responseBody = pm.response.text();
    console.log("Received Response:", responseBody);

    xml2js.parseString(responseBody, { explicitArray: false, trim: true }, function (err, result) {
        if (err) {
            console.log("Error parsing response:", err);
            return;
        }

        // Extract <string> content
        if (result && result.string && result.string._) {
            var encodedXML = result.string._; // Extract actual XML string inside <string>

            // Decode XML entities
            var decodedXML = encodedXML.replace(/</g, "<")
                                       .replace(/>/g, ">")
                                       .replace(/&/g, "&")
                                       .replace(/"/g, '"')
                                       .replace(/'/g, "'");

            // Parse the cleaned XML into JSON
            xml2js.parseString(decodedXML, { explicitArray: false, trim: true }, function (err, finalResult) {
                if (err) {
                    console.log("Error parsing cleaned XML:", err);
                    return;
                }

                // Convert JSON back to XML
                var builder = new xml2js.Builder();
                var finalXMLString = builder.buildObject(finalResult);

                // Log and store the final cleaned XML
                console.log("Final Cleaned XML:", finalXMLString);
                pm.environment.set("cleanedXML", finalXMLString);
            });
        } else {
            console.log("No valid <string> element found.");
        }

        var cleanedXML = pm.environment.get("cleanedXML");
        // Transform
        xml2js.parseString(cleanedXML, { explicitArray: false, trim: true }, function (err, result) {
            if (err) {
                console.log("Error parsing XML:", err);
                return;
            }

            // Iterate over each <pStatus> and apply the transformation
            let statuses = result.BizAgiWSResponse.Entities.pStatus;

            // Ensure it's an array
            if (!Array.isArray(statuses)) {
                statuses = [statuses];
            }

            statuses.forEach(status => {
                if (status.kpStatusType) {
                    // Extract sCode and sName from kpStatusType
                    let sCodeValue = status.kpStatusType.sCode;
                    let sNameValue = status.kpStatusType.sName;

                    // Append them as new elements under pH_Status
                    status["kpStatusType_sCode"] = { _: sCodeValue };
                    status["kpStatusType_sName"] = { _: sNameValue };

                    // Remove the original kpStatusType and sName
                    delete status.kpStatusType;
                    delete status.sName;
                }
            });

            // Convert the updated JSON back to XML
            var builder = new xml2js.Builder();
            var transformedXML = builder.buildObject(result);

            // Store the transformed XML in Postman environment
            console.log("Transformed XML:", transformedXML);
            pm.environment.set("transformedXML", transformedXML);
        });
    });
});

Running the Transformation in Postman

Click send. The JavaScript code will extract the string response, transform it into an XML (cleanedXML – Dev Variable), and apply a certain transformation (transformedXML – Dev Variable).