1

I have a request with body like below:

"RequestBody": {
        "numbers": ["20030013", "10010701", "10006402"]
    }

And I'm trying to catch above request with Wiremock like:

{
"request": {
    "urlPattern": "<my_url_com>",
    "method": "POST",
    "bodyPatterns": [
        {
            "matchesJsonPath": "$.RequestBody[?(@.numbers == ['10010701'])]"
        }
    ]
},
"response": {
    "status": 200,
    "jsonBody": {
        "ResponseBody": {
            ...
        }
    }
}

} Because I'm interested in only when request contains "10010701".

But it works only if numbers contain only one value. So, how can I catch this request with multiple values?

S.I.D
  • 13
  • 3

1 Answers1

0

WireMock uses Goessener JSONPath, so we should be able to use JS inside of the ?() filter. We can then use the .indexOf() function to check if a value exists in an array. If it does, it will return the index of the item, and if it doesn't it will return -1. By checking that the value is not -1, we can assume the value exists in the array.

Info taken from here

"matchesJsonPath": "$.RequestBody[?[@.numbers.indexOf('10010701') != -1)]"
agoff
  • 5,818
  • 1
  • 7
  • 20
  • Thanks for glue. I managed to find '10010701' with `$.RequestBody.numbers[?(@.indexOf('10010701') != -1)]` and tried it with https://jsonpath.herokuapp.com/ but WireMock dosen't cach the request with this expression. There are any ideas? – S.I.D Dec 03 '21 at 16:37
  • https://jsonpath.herokuapp.com/ uses [Jayway JsonPath](https://github.com/json-path/JsonPath#jayway-jsonpath) which doesn't support `indexOf` – Akshay G Dec 07 '21 at 04:55
  • @AkshayG at jsonpath.herokuapp.com you can choose between Jayway,Gatling,Nebhale and Goessner notations – S.I.D Dec 08 '21 at 09:52