mockserver icon indicating copy to clipboard operation
mockserver copied to clipboard

Use regex in json request body

Open nshahid-mdsol opened this issue 2 years ago • 1 comments

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\": \".*\" }"
 				}
			}

nshahid-mdsol avatar May 20 '22 20:05 nshahid-mdsol

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.

BamdadNouri avatar May 24 '22 10:05 BamdadNouri

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

simonnepomuk avatar Mar 09 '23 15:03 simonnepomuk