我正在使用 XML 正文来处理 API 请求,并且我需要提取一个属性以通过 Webhook 发送。我已成功提取该属性并将其包含在 API 响应中(这反映了一切正常)。但是,当我尝试在 webhook 的serveEventListeners 中使用它时,遇到错误:attribute="[ERROR: null is not valid XML]"。 谁能告诉我如何解决这个问题?
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>
这也发布在 WireMock Community Slack 上并在那里回答 - https://wiremock-community.slack.com/archives/C03N1E6HFPY/p1716875712452979
为了完整性,也将答案发布在这里。 问题在于,在 webhook 主体中,使用了以下
xpath
帮助器:
{{xPath request.body '/ReqPay/Payer/@addr'}}
在 webhook 主体中引用请求时,您必须引用
originalRequest
而不是 request
:
{{xPath originalRequest.body '/ReqPay/Payer/@addr'}}
这记录在 WireMock 文档中 -