New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation confusing when it comes to variable scopes for local variables #4052

Closed
vdespa opened this Issue Jan 10, 2018 · 12 comments

Comments

Projects
None yet
7 participants
@vdespa
Copy link

vdespa commented Jan 10, 2018

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:

var foo = "bar";

The documentation gets more confusing after you read this:

The following scopes are available to you:

Global
Collection
Environment
Local
Data

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.

@sivcan sivcan added the support label Jan 11, 2018

@sivcan

This comment has been minimized.

Copy link
Member

sivcan commented Jan 11, 2018

Hi @vdespa.
Variables are simply used in Postman the way you actually use them in a programming language.

Let's look at an example :
Suppose you want to hit the 2-3 domains with a simple get request as :
url-to-be-hit/get?test=params-to-be-added

The way we'll be using it in the request builder is :
{{domain-url}}/get?test={{params}}

Now, for domain-url's you can set them as environment variables and all other requests under the environment will be able to use that variable.
env

You can 'edit' this and set the domain-url as per your need.
Also, you can easily add params and check what you need and what you don't need.
Now you can also, use variables inside params.
testvars

Now if you run the request, postman will resolve the variables and hit the correct url.
You can change the domain-url multiple times or using scripts. (pm.global / pm.environment) depending on the variables scope.
You can also use scripts to manipulate variables.

Scopes:

  • Environment variables can be accessed with the corresponding environment template.
  • Collection variables can be accessed from a request within the collection.
  • Global variables can be accessed broadly regardless of the selected environment.
  • Data Variables - If you've used the Collection Runner then it allows you to import a data file and then use the values from the data file inside HTTP requests and scripts. To use them inside Postman, follow the same syntax as environment or global variables.

Local : Variables that you'll be defining in the scripts and using them will be local to those requests.
Global variables will not be overwritten with local variables.

Hope this clarifies your issue.
Let me know if you need more information.

@sivcan sivcan self-assigned this Jan 11, 2018

@vdespa

This comment has been minimized.

Copy link
Author

vdespa commented Jan 11, 2018

Thanks for your long response.

I still consider the documentation confusing, as it puts the local variables in the same "bucket" as the rest of the variables.

@sivcan

This comment has been minimized.

Copy link
Member

sivcan commented Jan 11, 2018

@vdespa - No problem.
We'll look over the same and see what we can change to make it even more easier to understand.
I'll be closing this issue now.
If you feel that there's still anything that's not clear then feel free to reach out.

@sivcan sivcan closed this Jan 11, 2018

@mistaecko

This comment has been minimized.

Copy link

mistaecko commented Mar 22, 2018

The documentation is still confusing. No mention of pm.variables.set(key, value) which my guess is what the "local" scope refers to.

@vdespa

This comment has been minimized.

Copy link
Author

vdespa commented Mar 27, 2018

@mistaecko of course it is still confusing 😕

@verglor

This comment has been minimized.

Copy link

verglor commented Apr 25, 2018

@vdespa There actually is a local scope API:
pm.variables.set - is setting local scope variable
pm.variables.get - is getting local scope variable or closest scope variable if not set locally

Quite confusing - I agree.

@vdespa

This comment has been minimized.

Copy link
Author

vdespa commented May 6, 2018

@verglor Indeed your are right. I have missed that.

Thanks for pointing that out.

@BorysFursov

This comment has been minimized.

Copy link

BorysFursov commented May 16, 2018

@vdespa Also I noticed that Local VariableScope doesn't keep it's condition! It is clear again when next request starts

@p00j4

This comment has been minimized.

Copy link

p00j4 commented Jul 18, 2018

@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

  • As it's scope is only within the same request and hence not useful to get in another request (its scope is lost)
  • also I couldn't figure the usecase for pm.variables because within the same request, could go directly with javascript variable. Edit: oh Yes, this one is fine, this can be useful in passing to the same request building from per-request script if any.

Now only query is, which way can use Collection level variables can be created using the postman scripts on the fly?
The doc here talks about per-defining the collection level variables which is not useful in above case

@sivcan

This comment has been minimized.

Copy link
Member

sivcan commented Jul 19, 2018

@p00j4 Collection variables cannot be updated through the scripts.
The only way is to use environment variables/globals.

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.
You can read the public RFC here: https://community.getpostman.com/t/sessions-in-postman/1925

@tparikka

This comment has been minimized.

Copy link

tparikka commented Dec 12, 2018

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.

@sivcan

This comment has been minimized.

Copy link
Member

sivcan commented Dec 12, 2018

Hi @tparikka, we're working around improving our docs and as you can see here postmanlabs/postman-docs#1442, the issue is already open.
You follow the thread for updates regarding the variables docs.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment