How can I match against the wiremock server URL in the body of a POST?

Skip to first unread message

Mark Anderson

Aug 29, 2021, 9:59:01 AM8/29/21
to wiremock-user
I'm using Wiremock with a client calling the OSISoft PI Web API and I'm having issues matching against the POST body to the request defined at as with Wiremock the POST body will have a URL containing the randomly generated port. e.g.

      "1": {
        "Method": "POST",
        "Resource": "http://localhost:24353/piwebapi/assetdatabases/D0NxzXSxtlKkGzAaZhKOB-KABJ2buwfWrkye3YhdL2FOUAUEhMQUZTMDRcQgYUUEVSRk9STUFOQ0UgVEVTVElORw/elements",
        "Content": "{\"Name\":\"New Element\"}"

How can I match against the URL in the Resource element where the port number will change between test runs? Obviously I cannot use templating for requests like I would with responses.

From reviewing the docs on request matching I see that using ```equalToJson``` with placeholders or ```matchesJsonPath``` with a regex expression may be an option but I'm not clear on the exact syntax. Or is there a simpler option that I'm missing?


Tom Akehurst

Aug 29, 2021, 4:44:36 PM8/29/21
to wiremock-user
If you don't need to match anything in particular for that element you could use a JSON placeholder e.g. "Resource": "${json-unit.any-string}".

If you do need to match the part after the port number then you can use the regex form e.g.: "Resource": "${json-unit.regex}.*/piwebapi/assetdatabases/D0NxzXSxtlKkGzAaZhKOB-KABJ2buwfWrkye3YhdL2FOUAUEhMQUZTMDRcQgYUUEVSRk9STUFOQ0UgVEVTVElORw/elements"}"
Reply all
Reply to author
0 new messages