Enable Dark Mode!
how-to-use-environments-and-variables-in-postman.jpg
By: Abhishek ET

How to Use Environments & Variables in Postman

Technical Odoo 16

If you are working on any integrations with Odoo, the most common tool that you would have used for testing the endpoints will be the Postman. Postman helps to test endpoints or REST APIs with different methods and authentication. It provides a very simple UI to manage the requests. It also provides different representational options for viewing the results.

Environments

In most of the projects, we will be maintaining multiple instances of Odoo. 1 for the development and 1 for the production instance. In some projects, we might add more instances for staging and UAT. We will have a different base URL for each instance. So, for the REST APIs in an instance, we should have to change the base URL in the requests. We can maintain this by using the Environments in Postman.

It is possible to create Environments in Postman.

How to Use Environments & Variables in Postman-cybrosys

To create a new Environment, click on Environments. Then, click on the + icon and provide a name for the new environment. There is a selection option on the top right corner to choose an environment while working in Postman.

How to Use Environments & Variables in Postman-cybrosys

Variables

We can use variables in Postman for storing and reusing values. In Postman, we have three kinds of variables:

1. Global variable

2. Environment variable

3. Collection variable

Each one of these variables has a different scope. The global variables can be used in all requests where the environment variables are limited within the environment and the collection variables are limited within the collections.

To create the global variables, navigate to Environments, select Globals, and then add the required variables.

How to Use Environments & Variables in Postman-cybrosys

To create environment variables, select any of the environments and then add the required variables.

How to Use Environments & Variables in Postman-cybrosys

To create collection variables, navigate to Collections, select any of the collections, open the Variables, and then add the required variables.

How to Use Environments & Variables in Postman-cybrosys

In the case of global and environment variables, we can choose two types.

1. Default

2. Secret

We can see the values directly for the variables of default type, where we use the type secret for storing sensitive data to keep its values masked on the screen, like passwords. We have an option to view the value.

How to Use Environments & Variables in Postman-cybrosys

When we use the variables, we will work with the current value of a variable, which helps prevent sharing sensitive values with the team.

We can use these variables in the requests within {{}}. For example, if we want to use the base URL in the request, we can add {{base url}} in it.

How to Use Environments & Variables in Postman-cybrosys

Consider an example where we have two endpoints,

1. Endpoint for fetching a token.

How to Use Environments & Variables in Postman-cybrosys

2. Endpoint for fetching the invoice details of a customer using the token.

How to Use Environments & Variables in Postman-cybrosys

So, we have to fetch the token first using the first endpoint providing the username and password, and then copy the token from the response and use it as the Bearer token for the authentication of the second one. Let us see how we can improve these requests using variables and test scripts in Postman.

As displayed in the screenshot, the environment variables are set in the environments.

So, the values will be taken according to the active environment.

How to Use Environments & Variables in Postman-cybrosys

A test script is added, which will set the token variable in the response.

How to Use Environments & Variables in Postman-cybrosys

const responseJson = pm.response.json();
pm.environment.set("login key", responseJson.token);
pm.test("Test Login Key", function () {
pm.expect(responseJson.token).to.eql(pm.environment.get("login key"));
});

So, when we send the request, we will update the value of the environment variable login key.

The second request is also updated with the environment variables.

How to Use Environments & Variables in Postman-cybrosys

Once we set the variables properly for all the environments, we can use the requests in all instances by just changing the active environment.

Some effort on the configurations for the environments and proper usage of the variables and the test scripts will help us reduce the time and work on using the requests in Postman.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message