How to assign the generated token to a global variable and use of environments in Postman:
Copying the token and assigning it to different requests can be difficult. Instead, you can create a variable and assign the token to that variable. In your requests, you can use the variable directly. Simply trigger the token URL, and then proceed to trigger your GET or POST requests.
Generate Token and Assign to Global variable:
1. Click on the main collection, which will populate the right-side window. Select the "Variables" tab and add the variables you need.
For the Resource variable, I am assigning my dev machine URL.
2. For the AccessToken variable field, I want to capture the generated token after triggering the API.
3.Create an environment using the option at the top right side.
4.After creating the environment, select the highlighted option and click on "Edit." Add the variable and save it.
5. To capture the token, write the following JavaScript code under the "Scripts" tab of the API request that generates the token:
var access_token = pm.response.json().access_token; pm.environment.set("accessToken", access_token)
6. After clicking the "Send" button, the system will assign the generated token to my "AccessToken" variable.
Different Environments:
Instead of creating different variables, we can create different environments and maintain different values for the same variable based on the environment.
1.On the top-right corner, you will find the Environment window. Click on the + symbol to create a new environment.
2. Alternatively, you can click on the Environments option on the left side. Here, you will see the environments available in Postman.
3. You can create variables as required for your process.
4. Maintain the same variable name across all environments.
5. Use this variable in your request and click Send. For testing purposes, I have provided a dummy name.
Keep Daxing!!