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.
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.
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.
To create environment variables, select any of the environments and then add the required variables.
To create collection variables, navigate to Collections, select any of the collections, open the Variables, and then add the required variables.
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.
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.
Consider an example where we have two endpoints,
1. Endpoint for fetching a token.
2. Endpoint for fetching the invoice details of a customer using the token.
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.
A test script is added, which will set the token variable in the response.
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.
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.