Search code examples
muledataweavemulesoftmule4

"Script" 1 : 1" evaluating expression error in mule 4


I am trying to set the payload with a soap envelope, header, and body.

then substring payload by payload.indexOf("?>")+2

Based on the following expression:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://decisionresearch.com/RateMaker">
<soapenv:Header/>
<soapenv:Body>
#[payload.substring(payload.indexOf("?>")+2)]
</soapenv:Body>
</soapenv:Envelope> 

Input Request: https://github.com/Manikandan99/Map_request/blob/main/Request_map.xml

Expected output: https://github.com/Manikandan99/Map_request/blob/main/Response_map.xml

Mule FLow:

<flow name="map_requestFlow" doc:id="21d0d652-3766-47ed-95aa-a451f62c5776" >
        <http:listener doc:name="Listener" doc:id="7312114b-2857-40b3-98cd-f52b628b3a28" config-ref="HTTP_Listener_config" path="/map"/>
        <ee:transform doc:name="Transform Message" doc:id="7f061325-473b-4f63-8e95-990827b03259" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/xml
---
payload]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <logger level="INFO" doc:name="Logger" doc:id="21051f39-bb9f-412c-af73-f65051317757" message="#[payload]"/>
        <set-payload value='#[&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://decisionresearch.com/RateMaker"&gt;
&#10;&lt;soapenv:Header/&gt;
&#10;&lt;soapenv:Body&gt;
&#10;#[payload.substring(payload.indexOf("?&gt;")+2)]
&#10;&lt;/soapenv:Body&gt;
&#10;&lt;/soapenv:Envelope&gt;]' doc:name="Set Payload" doc:id="faf2bc3b-e7a3-4708-910b-daeb05bb5f6e" />
</flow>

Error message : https://github.com/Manikandan99/Map_request/blob/main/Error.txt

How to encapsulate soap with current payload?


Solution

  • That's wrong because of several reasons. First you can not use an expression inside an expression. Second you are trying to generate XML with strings, when DataWeave already does generates valid XML. Using strings directly is very very error prone. Whenever you see string manipulation to generate or parse XML, you should be questioning the reason.

    Instead use DataWeave to generate the XML completely in a simple transform:

    %dw 2.0
    output application/xml
    ns soapenv http://schemas.xmlsoap.org/soap/envelope/
    ---
    {
        soapenv#Envelop: {
            soapenv#Header: null,
            soapenv#Body: payload
        }
    }
    

    Output (assuming the input has format application/xml):

    <?xml version='1.0' encoding='UTF-8'?>
    <soapenv:Envelop xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
      <soapenv:Header/>
      <soapenv:Body>
        <ns0:rate xmlns:ns0="http://decisionresearch.com/RateMaker">
          <ns0:BrokerData>
            <ns0:taxa>TOPA/CISG01/ALL</ns0:taxa>
            <ns0:effectiveMode>effective</ns0:effectiveMode>
            <ns0:requestDate>20190101</ns0:requestDate>
            <ns0:function>rate</ns0:function>
          </ns0:BrokerData>
          <ns0:RateRequest>
            <ns0:Policy>
              <PolType>CNPK</PolType>
              <ClassCode>CANDIS</ClassCode>
              <Zip>80003</Zip>
              <State>CO</State>
              <EffDate>20211117</EffDate>
              <ExpDate>20221117</ExpDate>
              <Payroll>1200</Payroll>
              <OHPayroll/>
              <WAPayroll/>
              <FTE>5</FTE>
              <OccAgg>1000000/2000000</OccAgg>
              <DmgPrem>500000</DmgPrem>
              <MedPay>10000</MedPay>
              <EmpBen>Yes</EmpBen>
              <StopGap>None</StopGap>
              <OHStopGap>None</OHStopGap>
              <WAStopGap>None</WAStopGap>
              <HNO>1000000</HNO>
              <AssBat>100000/200000</AssBat>
              <PerLocAgg>Yes</PerLocAgg>
              <NumLoc>1</NumLoc>
              <WSUB>0</WSUB>
              <PICO>No</PICO>
              <AIML>0</AIML>
              <AIVS>0</AIVS>
              <AIPA>0</AIPA>
              <AIMA>0</AIMA>
              <AILL>0</AILL>
              <LLEA>0</LLEA>
              <AIDP>0</AIDP>
              <BAIV>No</BAIV>
              <WACombo>No</WACombo>
              <IRPMGL>2</IRPMGL>
              <LPDPGL>1.00</LPDPGL>
            </ns0:Policy>
            <ns0:Location>
              <LocationRef>Location-465697646-800339871</LocationRef>
              <State>CO</State>
              <Zip>80003</Zip>
            </ns0:Location>
            <ns0:Class>
              <Id>Risk-1296379588-1802098261</Id>
              <Number>1</Number>
              <ClassCode>CANDIS</ClassCode>
              <Exposure>2000000</Exposure>
              <GLBR>25</GLBR>
              <IsPrimary>Yes</IsPrimary>
            </ns0:Class>
          </ns0:RateRequest>
        </ns0:rate>
      </soapenv:Body>
    </soapenv:Envelop>