In this post, we will see, how we can use variables in JSON request body to make mock server more useful for our needs. Mock server request body matching works in the same way as we compare response body by using Default Validator in vREST.
We will now cover various scenarios to match JSON request body:
If we want to return some API mock response based on static JSON request body then in that case, simply define the static JSON request body in API Mock >> Request tab >> Raw Body sub tab.
Now, in the incoming mock request, if the request URL and method matches and request body of the incoming mock request matches exactly with the JSON body specified in the above image then mock server will return the response specified in the response tab of this API mock.
Now, suppose in scenario 1, we don't want to compare the value of name and email property in the JSON request body then we may write the raw body like this by using special variable {{*}}:
Now, suppose we want to validate the existence of property name and want to compare the value of email property and we want to ignore rest of the properties in the JSON request body, then we may write the raw body like this:
Now suppose we want to extract the name and email property from the incoming mock request and we want to use these properties in constructing the response of our Mock API. First we will see, how we may extract values from incoming JSON request body:
To extract the name and email property from the request JSON body, simply declare them as variables in the JSON request body as shown below.
Now, you may use these extracted variables in the API mock response like this: