Search code examples
xmlwiremockstubbingwiremock-standalonewiremock-record

Fetching attribute value from an xml request in wiremock and sending it as a webhook giving error-> [ERROR: null is not valid XML]


I'm working with an XML body for an API request, and I need to extract an attribute to send via a webhook. I've successfully extracted the attribute and included it in the API response(which is reflecting all fine). However, when I try to use it in the webhook's serveEventListeners, I encounter an error: attribute="[ERROR: null is not valid XML]". Could anyone advise me on how to resolve this?

XML Request:
<ns2:ReqPay xmlns:ns2="http://npci.org/upi/schema/">
    <Head ver="2.0" ts="2021-01-17T11:18:11+05:30" orgId="929390493" msgId="randomMsgId"/>
    <Payer addr="shikhar@gios" name="Bobi Jacob" seqNum="1" type="PERSON" code="0000">
    </Payer>
</ns2:ReqPay>

Admin Mapping:
{
    "request": {
        "method": "POST",
        "urlPattern": "/testing",
        "headers": {
            "Content-Type": {
                "equalTo": "application/xml"
            }
        }
    },
    "response": {
        "status": 200,
        "headers": {
            "Content-Type": "application/xml"
        },
        "transformers": [
            "response-template"
        ],
        "body": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ns2:Ack xmlns:ns2=\"http://npci.org/upi/schema/\" api=\"npciApi\" reqMsgId=\"{{randomValue length=32 type='ALPHANUMERIC'}}\" addr=\"{{xPath request.body '/ReqPay/Payer/@addr'}}\" ts=\"{{now format='yyyy-MM-dd\\'T\\'HH:mm:ssXXX'}}\"/>"
    },
    "serveEventListeners": [
        {
            "name": "webhook",
            "parameters": {
                "method": "POST",
                "url": "https://nnccdd.requestcatcher.com/testing0100",
                "headers": {
                    "Content-Type": "application/xml"
                },
                "delay": {
                    "type": "fixed",
                    "milliseconds": 800
                },
                "body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ns2:RespPay xmlns:ns2=\"http://npci.org/upi/schema/\" xmlns:ns3=\"http://npci.org/cm/schema/\">\n\t<Head msgId=\"xxx\" orgId=\"NPCI\" ts=\"{{now format='yyyy-MM-dd\\'T\\'HH:mm:ssXXX'}}\" ver=\"2.0\"/>\n\t<Ref addr=\"{{xPath request.body '/ReqPay/Payer/@addr'}}\" />\n\t</Resp>\n</ns2:RespPay>"
            }
        }
    ]
}

API Response i got:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Ack xmlns:ns2="http://npci.org/upi/schema/" api="npciApi" reqMsgId="e4gqglqg1xadizkpnvkr3ls1j5666pjk" addr="arunl@gip1" ts="2024-05-28T05:39:52Z"/>

Webhook response i got:
<?xml version="1.0" encoding="UTF-8"?>
<ns2:RespPay xmlns:ns2="http://npci.org/upi/schema/" xmlns:ns3="http://npci.org/cm/schema/">
    <Head msgId="xxx" orgId="NPCI" ts="2024-05-28T05:39:52Z" ver="2.0"/>
    <Ref addr="[ERROR: null is not valid XML]" />
    </Resp>
</ns2:RespPay>

Solution

  • This was also posted on the WireMock Community Slack and answered there - https://wiremock-community.slack.com/archives/C03N1E6HFPY/p1716875712452979

    Posting the answer here as well for completeness. The issue is that in the webhook body, the following xpath helper is used:

    {{xPath request.body '/ReqPay/Payer/@addr'}}
    

    When making reference to the request in the webhook body, you have to reference originalRequest rather than request:

    {{xPath originalRequest.body '/ReqPay/Payer/@addr'}}
    

    This is documented in the WireMock docs here - https://wiremock.org/docs/webhooks-and-callbacks/#using-data-from-the-original-request