hi <@U053QJ8NGNS> &amp; <@U0592TMJU4V> I've to se...
# wiremock-java
d
hi @Bas Dijkstra & @Lee Turner I've to send a webhook body which as follows- { "xml": "<xml><key1>mainvalue</key></xml>" } but need to update key 'key1' value from one of the key of request body. Need your insight
I'm trying this - {{xPath request.body '$.xmlData' '//request/key1/text()'}}
l
You need to reference originalRequest instead of request. https://wiremock.org/docs/webhooks-and-callbacks/#using-data-from-the-original-request
d
let me give a try to it
it's not working
l
You might need to post more information if we are to help any further. Can you post your whole mapping file and the request you use ?
d
Request body { “xmlData”: “<EDoc> <Msg ID="Edoc"> <Header> <Sender>sd</Sender> <Receiver>re</Receiver> <Priority>p</Priority> <MsgId>msg</MsgId> <Date>2024-05-08T145641</Date> <System>LITAS-MIG</System> </Header> </Msg> </EDoc>” } Webhook needed to be sent { "xml": “xmlData”: “<EDoc> <Msg ID="Edoc"> <Header> <Sender>sd</Sender> <Receiver>re</Receiver> <Priority>p</Priority> <MsgIdW>msg</MsgIdW> <Date>2024-05-08T145641</Date> <System>LITAS-MIG</System> </Header> </Msg> </EDoc>” } In webhook, value of tag MsgIdW should be replaced by request body's MsgId tried this- {{xPath request.body '$.xmlData' '//MsgId/text()'}}
l
Are you using the json mapping files or configuring your stubs by code? If json, could you post your mapping file please so we don't have to recreate it all from scratch
Either way, I think one problem could be that you are embedding XML data in JSON. You might need a combination of the
xPath
helper along with the
jsonPath
helper. Is there a reason why the XML has to be embedded in json rather than just passing the XML directly?
🙌 1
d
I am using JSON mapping file
actually it was coded way back, that way it's kept till now
but how we've to make that combo of xPath & JSON, that's little tricky
l
Can you post your json mapping file
d
{ "request": { "urlPathPattern": "/wiremock/request", "method": "POST", "bodyPatterns": [ { "matchesJsonPath": { "expression": "$..xmlData", "contains": "Sender>sd" } } ] }, "response": { "status": 200, "bodyFileName": "folder-name/responseFlow.xml", "headers": { "Content-Type": "application/soap+xml; charset=utf-8" } }, "postServeActions": [ { "name": "webhook", "parameters": { "method": "POST", "url": "http://sample.com/sendpoint", "headers": { "Content-Type": "application/json" }, "body": "{\n\"xml\": “xmlData”: “<EDoc> <Msg ID=\"Edoc\"> <Header> <Sender>sd</Sender> <Receiver>re</Receiver> <Priority>p</Priority> <MsgIdW>msg</MsgIdW> <Date>2024-05-08T145641</Date> <System>LITAS-MIG</System> </Header> </Msg> </EDoc>”\n}" } } ] }
@Lee Turner /@Bas Dijkstra guys pls let me know if any help can be made
l
This seems to work for me:
Copy code
{
  "request": {
    "urlPathPattern": "/wiremock/request",
    "method": "POST",
    "bodyPatterns": [
      {
        "matchesJsonPath": {
          "expression": "$..xmlData",
          "contains": "Sender>sd"
        }
      }
    ]
  },
  "response": {
    "status": 200,
    "bodyFileName": "folder-name/responseFlow.xml",
    "headers": {
      "Content-Type": "application/soap+xml; charset=utf-8"
    }
  },
  "serveEventListeners": [
    {
      "name": "webhook",
      "parameters": {
        "method": "POST",
        "url": "<https://yourcallbacksite.com>",
        "headers": {
          "Content-Type": "application/json"
        },
        "body": "{\n\"xml\": "xmlData": "<EDoc> <Msg ID=\"Edoc\"> <Header> <Sender>sd</Sender> <Receiver>re</Receiver> <Priority>p</Priority> <MsgIdW>{{xPath (jsonPath originalRequest.body '$.xmlData') '/EDoc/Msg/Header/MsgId/text()'}}</MsgIdW> <Date>2024-05-08T14:56:41</Date> <System>LITAS-MIG</System> </Header> </Msg> </EDoc>"\n}"
      }
    }
  ]
}
d
okay, let me try this
👍 1
this is working, woah thanks a lot to @Lee Turner
🥳
l
Great to hear 🎉