Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
Documentation confusing when it comes to variable scopes for local variables #4052
First of all, what is a local variable anyway? The documentation refers to them, but no example is given. So my understanding is that a local variable is a variable that I defined inside a pre-request or tests section, like:
The documentation gets more confusing after you read this:
It seems that you cannot access local variables by using the Postman API or the request builder.
If this is the case, why does scope play any role? When can you override a global or environment variable with a local one?
Thanks for clarifying.
Let's look at an example :
The way we'll be using it in the request builder is :
Now if you run the request, postman will resolve the variables and hit the correct url.
Local : Variables that you'll be defining in the scripts and using them will be local to those requests.
Hope this clarifies your issue.
@sivcan having a confusion, how can we define and reuse a collection level variable using the script (pass between requests)?
Use case: In a collection, I have multiple requests and want to pass variable value updated from one request to another. Don't want to set in environment variable because they(my variables) are always changing and they are many.
Tried with, pm.variables.set(key, value) but
Now only query is, which way can use Collection level variables can be created using the postman scripts on the fly?
@p00j4 Collection variables cannot be updated through the scripts.
However, since you've mentioned that your variables keep changing frequently and I suppose you don't want to update them with dirty data, I think the sessions in postman might fit your use-case well.
I landed here because the documentation at https://learning.getpostman.com/docs/postman/environments_and_globals/variables/#variable-scopes is still very confusing. Lack of concrete examples of what these scopes mean, particularly local, is in my opinion a major shortcoming in that section of the docs.