How to create an environment generic request template with Postman? – DEVELOPPARADISE
17/07/2018

How to create an environment generic request template with Postman?

Postman Collection and Environments zip

Introduction

Postman is one of the most popular REST API clients which provides a complete workflow for API development. In this article, we are going to discuss the Postman Environment and its benefits.

When we want to manage API requests for different environments then we need to create separate postman collection for different environments and it is more painful,

  • When no of API function grows on every iteration
  • When we want to change the existing request details for all environments. 

Fortunately, Postman environment feature resolves all above maintenance issues and it helps us to create request template which can be used across multiple environments (i.e. Development, QA, etc.) without creating a separate collection for each.

Pre-requisites

To follow along with this article you required following things,

  • Basic knowledge of Http protocol
  • Basic knowledge of Postman
  • Postman Desktop app. If you are not installed already then you can get it from www.getpostman.com.

What is Postman Variable?

To understand the Postman Environment better we need to understand the Postman variable. Postman variable is a key-value pair like a dictionary item. Key is the name of the variable and value is the actual value of a variable. Postman uses the double curly braces syntax to represents a variable. Postman will replace it with value at runtime (before sending a request to the server)

Syntax:

{{variable}}

What is Postman Environment?

Postman Environment represents the API/service host/runtime environment like Dev, QA, etc. It has one or more postman variables with an isolated scope. We can use these variables in any request parts like URL, headers, and body to create environment generic request template.

Example Scenario:

Assume you have an API for product management and it provides various API functions for product management. We are going to use product details API function for our demonstration.

Product detail API:

Verb: GET

Url: https://www.products.com/api/v1/products/<product id>

Headers:

x-api-key: <api key>

Example:

https://www.products.com/api/v1/products/2

Since the above API physically not exists I am going to use the httpbin.org to demonstrate how the Postman environment works. Httpbin.org provides a collection HTTP echo APIs and it will return the request details in the HTTP response so it will be the right choice to understand the postman environment.

To use the postman environment, we need to do the following major steps

  • Identify the environment specific elements from the API/service requests
  • Create the Postman environment specific to an API host/runtime environment. 
  • Create a variable under an environment for each environment specific elements which identified in step 1
  • Create an environment generic request template with help of newly created environment.
  • Run your requests across multiple environments.

Based on our above products API example, we can say domain and API key are going to change specific to an environment so we can create host and api_key variables under an environment.

How to create a postman environment?

To the following steps to create an environment,

  • Open the postman desktop app. Once it opens, you can find a small settings icon at the top right-hand side of the Postman window. Click on the Settings icon.
  • Manage Environments popup will open and it will show an empty list when you are opening for the first time.
    How to create an environment generic request template with Postman?
  • Click on Create an environment link from the help text or Add button which is available at bottom of the popup.
  • Add environment form will open. First, you need to enter the environment name and then required variable names and values.
  • Based on our example, create an environment with the following steps
    • Enter Development in the environment name field
    • Type host in the first-row new key field and type dev.products.com in the value field
    • Type api_key in the second-row new key field and type xxxxxxxxxx-dev-xxxxxxxxxx in the value field
    • Click on Add button.
      How to create an environment generic request template with Postman?
    • It will back to Manage Environment popup. Now, it will list out newly created development environment in the list
      How to create an environment generic request template with Postman?
    • To understand the Postman environment feature better we need to create at least one more environment. This time we no need to create the environment from the scratch. We can use the Duplicate Environment option.
    • Click on the Duplicate Environment icon at Development environment row
      How to create an environment generic request template with Postman?
    • Click on Development Copy environment
      How to create an environment generic request template with Postman?
      How to create an environment generic request template with Postman?
    • Change the environment name to QA
    • Change the host value to qa.products.com
    • Change the api_key value to xxxxxxxxxx-qa-xxxxxxxxxx
    • Click on Update button
      How to create an environment generic request template with Postman?
    • It will return to Manage Environment popup. Now you can see both Development and QA environments
      How to create an environment generic request template with Postman?

Once you created the required environments close the Manage Environment Window.

How to create a postman environment generic request template?

Now, we are ready to create the environment generic template with newly created Postman environments. Do the following steps create the request template.

  • Choose the Development from the Environment drops down.
    How to create an environment generic request template with Postman?
  • Type https://httpbin.org/get/?url=https://{{host}}/api/v1/products/2 in the URL field. You can notice as soon as you type the double curly braces Postman shows the variable intellisense, you can choose a required variable from here.How to create an environment generic request template with Postman?
  • Go to Headers Tab. Type x-api-key in key field and {{api_key}} in value field.
    How to create an environment generic request template with Postman?

    How to create an environment generic request template with Postman?

  • Now we are ready to test our request, before that we need to open the Postman console to see the request log.
  • Click on View -> Show Postman Console or press Alt + Ctrl + C. It will bring the Postman Console and keep it aside.
    How to create an environment generic request template with Postman?
  • Click on the send button to send the request to httpbin.org API.
  • Go to the Postman console to see the request log. If you can see that Postman replaced all variables with actual values in the URL and header section.
    How to create an environment generic request template with Postman?
  • Go back to the Postman main window to see the httpbin.org response. It also confirms the same result what we are seen in the console window.
    How to create an environment generic request template with Postman?
  • You can reuse the same request for the different environment by changing the environment.
  • To switch to another environment, you need to change the environment dropdown. Change the environment to QA that is it, now you can run the same request in the different environment.
    How to create an environment generic request template with Postman?
    How to create an environment generic request template with Postman?

Postman Environment feature made our life easy, we can jump into a different environment by simply changing the environment dropdown. 

Conclusion

In the software development world, maintenance is the most important quality factor, so wherever it is possible, we need to improve it and postman environment feature help us to effectively maintain our API test requests for a different environment. Thanks for reading this article. Please share valuable your feedback to improve my writing and sharing the knowledge.