Introducing support for templating in Postman mock servers
We’re continuously striving to advance the Postman API Platform with the tools and solutions that will help you build your APIs easily and better accomplish your goals. With that mission in mind, we’ve now added support for templating in mock servers, enabling you to get contextual responses to mock requests quickly.
In this blog post, we’ll quickly show how you can define collection examples using template helpers and what is now available for you as part of these helpers. Let’s get started.
Helpers that you can use while defining templates
$body
$queryParams
$pathSegments
$headers
The helpers mentioned above give you access to the request data sent to the mock server. You can use object-path syntax to access specific values on these helpers, and you can also define a default value for a key that will get used if the mock server can’t resolve the variable.
Examples
{{$body}} - Access the complete request body {{$body 'path.to.property'}} - Access a specific property from body {{$body 'path' 'default value'}} - Define a default value for a property {{$body 'a\.a'}} - Access a.a key from the body, which itself has a '.' in the key // Similar syntax can be used with queryParams, pathSegments and headers.
Defining collection examples using templates
Step 1. Create a collection.
Step 2. Add a request to the collection.
Step 3. Define an example using helpers.
Step 4. Create a mock server on the collection.
Step 5. Call the mock server with any request data combinations.
Hi to create template for response and how we can create conditional response instead of static
Hey Lalit,
You can access the request body and create your responses using template helpers. You can define a default value for a property you want to access like this – {{$body ‘path’ ‘default value’}}. Putting conditions on request data while forming responses is not supported as of now. You can go ahead and open a feature request for the same here.
Hope you are finding the feature useful!
Hi Aman,
is there a way to dictate how we want request body matching to be done for Postman mock servers? Currently, it looks like when you set “Request body matching” on the Server configuration page to true, it does EXACT request body matching but this is not ideal (puts a lot of constraint).
Is there a way to configure this so that we have a partial match, or we only want to match the “important” fields.
Example: I want to match a request the comes in with the body: {“first-name”: X} with example X. But I don’t care if there are other fields in the request. For instance, {“first-name”: X, {“other_fields”: {…}}} should also match to the same Example X. I only want to match certain fields…
Is it possible to configure this with current Postman features?
What if the request has multiple child items and the response must have respective child item value.
eg: {“parent”:[{“child1″:”1”, “child2″:”2”}, {“child3″:”3”, “child4″:”4”}]}
What is the patch of Child 3 now?
Please contact our support team at http://www.postman.com/support, and they’ll be able to help you.