Hi team, Need small help here. I have XML as the ...
# help
j
Hi team, Need small help here. I have XML as the request. I want to match the XML tags in the request. but the error is always body not matched when I run this. If anyone can pls help. Here is an example of my XML - <?XML version="1.0"?> <Getlocation xmlns="HTTP://namespace"> <searchdetails> <Search> <City>New York<City? <countrycode>xx<countrycode> <searchdetails> My mapping file looks like the below: { "priority": 1, "request": { "method" : "GET", "URLPATH" :"/send-me", "bodypatterns" : [{ "matchesXPath":"searchdetails[city=\"New York\" or countrycode=\"xx"\"]", "xPathNamespaces" :{ "xlmns" : "HTTP://namespace" } }] }
l
Hi Ron Not sure if something happened when you pasted the example xml but it doesn’t look like it is valid xml. I have update the xml for the purposes of this example based off what you posted:
Copy code
<Getlocation>
    <searchdetails>
        <Search>
            <City>New York</City>
            <countrycode>xx</countrycode>
        </Search>
    </searchdetails>
</Getlocation>
You can then use that with a mapping like this to match the request:
Copy code
{
  "priority": 1,
  "request": {
    "method": "GET",
    "urlPath": "/send-me",
    "bodyPatterns": [
      {
        "matchesXPath": "/Getlocation/searchdetails/Search/City[. = \"New York\"]"
      }
    ]
  },
  "response": {
    "status": 200
  }
}
I haven’t added the namespace definition in there but hopefully the above will get you started
If you wanted to match on
City
or
countrycode
like in your example you could do something like this:
Copy code
"matchesXPath": "/Getlocation/searchdetails/Search[City/text() = \"New York\" or countrycode/text() = \"xx\"]"
j
Appreciate your help Lee.. I will try this in a while. Thanks again
👍 2
Hi @Lee Turner and @Oleg Nenashev - Thank you for the inputs last time. I tried the feedback but still not get this worked. I pasted my XML below and the mapping file. It is manually typed so please ignore any formatting errors. Since I am very new to Wiremmock. Just to understand a few more details. The request xml is to be placed in the _file folder and Wiremock will search for the path mentioned in the "matchedXpath" and if matched it will use it as a request. Thank you again for the help.
XML: <?Xml version="1.0"?> <GetLocationRequestRequest xmlns="http://URLpATH""> <SearchLocationDetails> <SearchCriteria> <City>Boston</city> <Countrycode>US</Countrycode> <StateProvice>MA</StateProvice> Here is my mapping file: { "request" : { "Method"" GET "url" : "/send-file", "bodyPatterns" : [ { "matchesXPath": "//searchcriteria[city=\"Boston\" or countrycode=\"US\"]", "xPathNamespaces" : { "ex" : "http://URLpATH" } }] }, "response" : { "status" : 200, "headers": { "Content-Type": "text/xml;charset=UTF-8" }, "body" : "<Abody>" }} And even tried the below: { "priority": 1, "request": { "method": "GET", "urlPath": "/send-me", "bodyPatterns": [ { "matchesXPath": "/GetlocationRequestRequest/searchLocationDetails/SearchCriteria/City/text()= \"Boston\"]" or countrycode/test() =\"US\"], "xPathnamespaces" :[ "xmlns": "http://URLpATH" } ] }, "response" : { "status" : 200 } }
l
So, the request xml is used in the request you send to wiremock. You can send the request using something like postman or curl
j
Yes thats what I am doing. I am sending the request via Postman using the URL...it works for other types of requests (simple ones) but not working for the above. It keeps saying Body not found in the request
l
Not sure what the problem would be. I have used the mapping file below:
Copy code
{
  "request": {
    "method": "GET",
    "url": "/send-file",
    "bodyPatterns": [
      {
        "matchesXPath": "//SearchCriteria[City/text() = \"Boston\" or Countrycode/text() =\"US\"]"
      }
    ]
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "text/xml;charset=UTF-8"
    },
    "body": "<Abody>"
  }
}
With this request payload and it seems to work:
Copy code
<GetLocationRequestRequest xmlns="<http://urlpath.org>">
    <SearchLocationDetails>
        <SearchCriteria>
            <City>Boston</City>
            <Countrycode>US</Countrycode>
            <StateProvice>MA</StateProvice>
        </SearchCriteria>
    </SearchLocationDetails>
</GetLocationRequestRequest>
I don’t think you need the
xPathnamespaces
in your mapping file.
Cursor_and_wiremmock_example_-_CBSConnect.png
j
Ohh. this is my mistake. I did not even kept anything in the Body of the postman. Sorry I am not the API/postman guy. I thin this should work. Many thanks Lee. I will try this.
l
I don’t think I added anything in the headers. I am out at the moment so not on my laptop but can check when I get back.
j
You are correct. I see postman added it itself.
l
Awesome. Glad it works.