Introducing support for templating in Postman mock servers

Avatar

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.

You can use the New or + button to create a collection

Step 2. Add a request to the collection.

Add a request to the collection

Step 3. Define an example using helpers.

Utilize $body helper to access the username key in the example response

Step 4. Create a mock server on the collection.

Create a mock server on the Login API collection

Step 5. Call the mock server with any request data combinations.

The username key in response was resolved using the request’s username value

Try Postman now

What do you think about this feature? Tell us in a comment below. You can also give product feedback through our Community forum and GitHub repository.

Comment

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

5 thoughts on “Introducing support for templating in Postman mock servers

  • Avatar

    Hi to create template for response and how we can create conditional response instead of static

    • Avatar

      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!

  • Avatar

    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?

  • Avatar

    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?