But hopefully this use case (especially with the recent change to enable write access to collection variables from scripts) highlights my argument for collection variables being a more specific scope than environment variables (and thus, being used when there are variables of the same name for both scopes)
1st: local, which can be used across an execution (as specific as a single request)
2nd: collection, which can be used across a collection
3rd: environment, which can be used across collections
4th: global, which can be used across collections and across environments
Hi there
I to think it not very usefull, that environment variables take precedens over collection variables. As @morleym_cubic expresses, collection is a narrower scope than environment. By the current behavior, it is not possible to override an evironment variable with a collection variable. So we can not have different collections that uses local values to replace default values in the environment.
According to Postman docs: -and-environments/variables/#variable-scopes , collections are a narrower scope than environments and a collection variable should take precedence over an enviroment variable. The principle is mentioned in the docs for global and local:
Actually, I do agree that Environment variables take precedence over Collection. We set the default (and published as public) variables in Collection. But for our testing and staging servers etc, we can override these by creating Environment variables, which are all non-public.
I think the problem is more about how the software/UI is designed. It gives a sense that Environments are the top level, because they are right there in the main navigation.
My team members and I have a lot of variable synching issues. We currently only use Environment variables as we monitor all or our collections. We all use the Postman App itself, but I also use the Web version for running monitors manually. We have tried a couple of different processes, and our issue is intermittent.
I'm using the packaged app version of Postman to write tests against my Rest API. I'm trying to manage state between consecutive tests. To faciliate this, the Postman object exposed to the Javascript test runtime has methods for setting variables, but none for reading.
Now, I can read this value in the next call via the key structure that sucks values in from the current environment. BUT, this doesn't work in the tests; it only works in the request building stuff.
Once a variable has been set, use the pm.variables.get() method or, alternatively, use the pm.environment.get() or pm.globals.get() method depending on the appropriate scope to fetch the variable. The method requires the variable name as a parameter to retrieve the stored value in a script.
A very common scenario while testing APIs is that the API infrastructure might be present on your local machine, a staging setup, and a production setup. Using variables, you can call all these APIs without having to manually replace values. You can also use variables to test for different users (for example, they might need different access tokens), different input values, and a lot more.
With Jetpacks, you get even more power to play with variables. You can extract data from responses and set variable values automatically. You can chain requests one after the other with minimal effort. Variable values are also available inside tests to help you make them more generic.
Data variables let you test multiple instances of a request in one go. Imagine that you want to test an API that lets users post comments on a post. Using data variables, you can test for hundreds of variations of a request with different IDs, tokens or content bodies.
Once an environment is selected, Postman will replace all instances of a variable with its corresponding value. If no environment is selected, then Postman will try to find a matching global variable.
Inside Postman test scripts, you can set environment and global variables using the postman.setEnvironmentVariable and postman.setGlobalVariable functions. You can also access these values using the special environment and globals dictionaries.
Check out our blog post about how you can extract values from response bodies and assign them to variables. Subsequent calls are made using these values. This is an extremely powerful feature and I am sure you will wonder how you went by without it!
Data variables are used inside the Collection Runner. The Collection Runner can run a collection or a folder multiple times. For every iteration, it will pick up new values from a JSON or a CSV file and replace variables in the request with these values.
Using variables is essential to maximizing your Postman workflow. I hope this tutorial helped you gain a better understanding of this feature. If you have any questions, do let me know in the comments below.
Hi Steve,
File uploads are an issue. We are trying to figure out what will be the best way to provide this feature. One idea is to have a file repository within Postman and then refer to these within requests as variables. Issue is that storing/loading files inside Postman might slow it down, hence we want to be sure before introducing this feature.
Perhaps another possibility would be to allow file references by URI? It would require a fetch first, but at least it would allow collection runners with file upload fields to be used. In this way, too, the file URI could be used in a data file or environment as easily as any other variable.
An environment in Postman is a set of key-value pairs. An environment helps us to differentiate between the requests. When we create an environment inside Postman, we can change the value of the key value pairs and the changes are reflected in our requests. An environment just provides boundaries to variables. When we create different environment we can make track of all the variables and how to use them inside our requests. There can be many variables inside one environment. At once, we can work only in one environment although we can create any number of environments in Postman. The below screenshot shows three environments that we created.
A variable in the Postman is same as in any programming language. A variable is an entity whose value can be changed. The key part in the key-value set in environment is called variable. This variable can have any value and in place of the key we can use the variable name in every request. This will be clear with an example shown below and steps shown thereafter.
Now, we have created an environment and used a variable called URL here. This variable can be now used instead of the actual URL. You can see the response which is same as before we were using the full URL.
A scope of anything is the boundary in which that thing can be accessed and perform. For example, if you are an engineer and do not have a passport, your scope is limited to India since you cannot go outside. While having a passport changes your scope to the world. Similarly, variables in Postman have two scopes
Local Scope Variables can only work inside the environment in which it was created. Changing the environment will stop the access to that variable and we will encounter an error.
The variable URL that we just created above is the local variable because it has a scope only till the environment Weather API. In the following steps we will explore the limitations of local variable by accessing local variable in other environment, where it is not present.
This error occured because Postman does not know about URL variable because we have changed the environment. Therefore, URL is a local variable having scope only till the Weather API environment.
Global Scope Variables can work outside the environment also. They are global and it does not matter which environment is selected. In the following image you can see three global variables by clicking the Eye icon.
For convenience Postman also has a feature which lets you see all the current variables and environment. Just click on Eye icon and it list down all the Environments and Global variables.
As we discussed, two global variables cannot have same name while two local variables can have same name provided they are in different environments. But what if one local variable and one global variable has same name? For example, you name a local variable ABC and a global variable ABC. Now when you select that respective environment both the variables will be activated. So, which will show its value? This confusion is solved by precedence.
Precedence generally means priority. While two or more things strike together, the one with higher priority (precedence) is preferred. In Postman for same name of environment specific variable and global variable, environment specific variable or local variable has higher precedence. It will overwrite the global one.
Here we have a problem, both the variables have same values. But if you look in the image above, global URL has been sliced off with a line. This has happened because both the variables have the same name and the precedence will be given to the local variable so global variable will not be used.
Both the variables are now accessible and can be used. If you press send now, you will get the correct response from global variable which you would get from local variable if they had same name. This is how precedence works.
Variables give the option to hold and repeat parameters in the requests, collections, scripts and so on. If we need to modify a value, we need to do it in only one place. Thus, the variables help to minimise the chance of errors and increase efficiency.
df19127ead