mockserver
mockserver copied to clipboard
Use regex in json request body
I am trying to create an expectation where I want to use regex in json body. As an example in the following json body I don't care what name and want to use regex. (This is an example for javascript).
"httpRequest": {
"method": "POST",
"body": {
"name": "[.*]"
}
}
As an alternative I have tried the following but this doesn't work for me.
"httpRequest": {
"method": "POST",
"body": {
"type": "REGEX",
"regex": "{\"name\": \".*\" }"
}
}
I actually have the same issue, my request's body has a couple of fields but only one of them is the kind of data I need to ignore (like date, it's not predictable and impossible to hard code), yet I need to validate the body and the regex does not work per field.
My use case was solveable by using JSONUnit Placeholders. Usage is documented here: https://www.mock-server.com/mock_server/creating_expectations.html#button_match_request_by_body_with_json_placeholders