If you're testing an API that uses XML payloads, such as SOAP APIs, you can validate responses and create variables using custom post-request scripts. Here’s a quick overview of the steps to accomplish this:
- Add a new API snippet to the post-request script
- Use the built-in xml2json library to convert the XML response body to a JSON object
- Use JSON path notation to iterate over object keys to find the target property
- Create a variable or an assertion using Postman
pm.methods
This article uses the Fahrenheit to Celsius endpoint from W3Schools to demonstrate how to work with XML payloads in the API Test Editor:
Add variables
This example will assert that the value of tempF is correctly converted to the value of tempC. Add the following variables to the Variables panel:
-
tempF=32 -
tempC=0
Configure request details
Add a request with the following details:
- Method: POST
- URL:
https://www.w3schools.com/xml/tempconvert.asmx - Headers:
- Content-Type = text/xml
- SOAPAction = https://www.w3schools.com/xml/FahrenheitToCelsius
In the Body tab of Request Details, add the following raw XML request body:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<FahrenheitToCelsius xmlns="https://www.w3schools.com/xml/">
<Fahrenheit>{{@tempF}}</Fahrenheit>
</FahrenheitToCelsius>
</soap:Body>
</soap:Envelope>
Send the request
When you send the POST request, the API returns an XML response body with the temperature of tempF in Celsius. In this example, the temperature is 0.
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<FahrenheitToCelsiusResponse xmlns="https://www.w3schools.com/xml/">
<FahrenheitToCelsiusResult>0</FahrenheitToCelsiusResult>
</FahrenheitToCelsiusResponse>
</soap:Body>
</soap:Envelope>
Validate the response
To assert that the value of <FahrenheitToCelsiusResult>0</FahrenheitToCelsiusResult> value equals the value of the tempC variable, add a new snippet to the Post-request tab in the Validation and variable assignment section.
The following script takes the following steps to validate the response:
- Convert the XML response body into a JSON object with the xml2Json library
- Use JSON path notation to get the value
FahrenheitToCelsius - Assert that the value of
FahrenheitToCelsiusequals the value of the variabletempC
pm.test("Validate conversion Fahrenheit To Celsius produces the expected result", () => {
//convert XML response body to a JSON object using xml2Json library
const responseJson = xml2Json(pm.response.text());
//Use JSON path syntax to get to the target property
let result = responseJson["soap:Envelope"]["soap:Body"].FahrenheitToCelsiusResponse.FahrenheitToCelsiusResult;
//Assert that result is the correct value
pm.expect(result).to.eql(pm.variables.get("tempC"));
});
Create a new variable
Creating a variable based on an XML response body requires the same initial steps that it takes to validate data in an XML-based response body: converting the XML to JSON with xml2json library and finding the target property value of FahrehheitToCelsiusResult with JSON path notation.
Use the pm.environment.set() method to assign the result to a variable.
//convert XML response body to a JSON object using xml2Json library
const responseJson = xml2Json(pm.response.text());
//Use JSON path syntax to get to the target property value to be assigned to a variable
let result = responseJson["soap:Envelope"]["soap:Body"].FahrenheitToCelsiusResponse.FahrenheitToCelsiusResult;
//Assign the result value to a given variable name
pm.environment.set("myTemperature", result);
To see the updated myTemperature variable value when you send the request, add myTemperature to the Variables list on the left side of the API Test Editor.