Karate embedded experssion to add XML blocks in the request payload transforms "<" to "&lt;"

Viewed 21

I have been trying to add a XML block in to XML payload, it works fine if I declare a variable with a XML block under the scenario before reading the XML file. However it fails/adds special characters replacing "<",">" if we fetch the address from Javascript function/another scenario in a different feature file.

Below works fine:

Scenario: Add address XML block
* def address = 
"""
    <Address>
              <StreetNo>2</StreetNo>
              <Street Type="Avenue">Second</Street>
              <City>ADELAIDE</City>
              <State Name="SA"/>
              <Postcode>5000</Postcode>
              <Country ISO3166="AU"/>
          </Address>
"""
* def b2bRequestBody = 
read('classpath:supportingFiles/b2bApplication_Post/CAL_11OwnsAndPurchase.xml')

This approach has problem:

Javascript function to return XML block:

function()
{
return {
fetchStdAddress1: function()
      {
       var Address =  
          `<Address>
              <StreetNo>2</StreetNo>
              <Street Type="Avenue">Second</Street>
              <City>ADELAIDE</City>
              <State Name="SA"/>
              <Postcode>5000</Postcode>
              <Country ISO3166="AU"/>
          </Address>`;
        return Address;
      },
}
}

Scenario to assign XML to a variable and reading XML file:

Scenario: Submit application from B2BApplication API
* def functions = call read('classpath:reusableFeatures/utils/customiseB2BRequests.js')
* def address = functions.fetchStdAddress1()
* print address
* def b2bRequestBody = 
read('classpath:supportingFiles/b2bApplication_Post/CAL_11OwnsAndPurchase.xml')
* print b2bRequestBody

Excerpt of XML payload:

<AddressWrapper>
<Identifier UniqueID="1655877356200" Type="BrokerAssigned"/>
    #(address)
</AddressWrapper>

Result: It is replacing "<" and ">" like below,

&lt;Address&gt;
    &lt;StreetNo&gt;2&lt;/StreetNo&gt;
    &lt;Street Type="Avenue"&gt;Second&lt;/Street&gt;
    &lt;City&gt;ADELAIDE&lt;/City&gt;
    &lt;State Name="SA"/&gt;
    &lt;Postcode&gt;5000&lt;/Postcode&gt;
    &lt;Country ISO3166="AU"/&gt;
&lt;/Address&gt;
 
1 Answers
Related