Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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:

Scenario 1: When we want to compare static JSON request body in the incoming mock request

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. 

Image Added

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.

Scenario 2: When we want to ignore values of some of the properties of the JSON request body in the incoming mock request

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 {{*}}:

Image Added

Scenario 3: When we want to only compare some specific properties of the JSON request body in the incoming mock request

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:

Image Added

Scenario 4: When we want to use some part of the incoming JSON request body in constructing the response of this mock API.

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:

Image Added