Skip to content

[SOLVED] Collection variables not working #4105

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

Closed
Amer21214 opened this issue Jan 25, 2018 · 44 comments
Closed

[SOLVED] Collection variables not working #4105

Amer21214 opened this issue Jan 25, 2018 · 44 comments
Assignees
Milestone

Comments

@Amer21214
Copy link

App Details:

Postman for Mac
Version 5.5.2
OS X 15.6.0 / x64

Issue Report:

  1. Did you encounter this recently, or has this bug always been there: Always
  2. Expected behaviour: When using {{variableName}} in a request where the variableName is set in the Collection Variables section, it is not being recognized in the request which is trying to use it.

scrsh1
scrsh2

@vdespa
Copy link

vdespa commented Jan 25, 2018

That is a known issue that will be fixed probably in the next release.
You can still use the variables in the request builder, even if they are marked as unresolved.

@Amer21214
Copy link
Author

Oh, awesome then! :)

@Amer21214 Amer21214 changed the title Collection variables not working [SOLVED] Collection variables not working Jan 25, 2018
@sdnts
Copy link

sdnts commented Jan 29, 2018

@Amer21214 As @vdespa said, these variables are not being populated in the autocomplete menus, but they do work. This has been fixed internally, and it will be out in the next release.

@geoffatcircle
Copy link

Not sure if this is solved. I realize this doesn't appear correctly in the UI, but it also doesn't work with shared collections or shared documentation

@afilp
Copy link

afilp commented Feb 23, 2018

Kind of different issue, can you please allow for a combo box in Collection Variables?

See here:

image

I would like to choose just one basse_url or token, so the values should be inside a combo box (needs some changes in the interface)

@Amer21214
Copy link
Author

Yeah, a new version came out and it still isn't solved. It isn't a "biggy" since the Collection variables do indeed work, it's just that people who start using it will be confused (like me) and think that these variables are not being recognized.

@prashantagarwal
Copy link

@Amer21214 What is the app version you are using ?

@prashantagarwal
Copy link

We have fixed these issues in our Canary channel. (https://www.getpostman.com/canary).

@Amer21214
Copy link
Author

Aaa, it seems to be working now in the "newest new" version. Awesome!!! 👍 :)

@sivcan
Copy link
Member

sivcan commented Mar 16, 2018

Awesome.

@adenix
Copy link

adenix commented Mar 19, 2018

This does not appear to be working in v6.0.10

screen shot 2018-03-19 at 9 38 50 am

screen shot 2018-03-19 at 9 38 18 am

screen shot 2018-03-19 at 9 39 11 am

@adenix
Copy link

adenix commented Mar 19, 2018

Nevermind. I have to save the request in the collection, close the request, and then re-open it.

@MikeNicholsPoints
Copy link

This is not working in 6.2.5 . If I create a collection variable firstName and try to use it in a body as {{firstName}} postman will hang for a moment and then throw the error

Error: Script execution timed out.↵    at 
ContextifyScript.Script.runInContext (vm.js:53:29)

@m3dbedb
Copy link

m3dbedb commented Nov 20, 2018

Same issue in Postman 6.5.2 for windows 7 x86.
I have initial collection variable named "serviceid". Initially I used environment variables and everything worked. Now I'm trying to use collection variables.
I can access it via

console.log(pm.variables.get("serviceid"));

but setting collection variable fails:

pm.variables.set("serviceid", jsonData.data[0].iname);

pm.variables.set("serviceid", "19");

Update
I works, but the app does't update values in collection variables editor (look at attached picture)

If you press update postman will reset current values with shown on screen
default
.

@sivcan
Copy link
Member

sivcan commented Nov 20, 2018

@MikeNicholsPoints Apologies for the late reply, can you provide us with more information and possibly the code snippet?
It probably looks like a different issue. Also, read further below in case you're facing the same thing.

@m3dbedb
Collection variables are read-only in the scripts.
You can only define (or edit) them only when you're manually editing the collection.
That's why 'get' works and 'set' for them doesn't work.

Docs: https://learning.getpostman.com/docs/postman/environments_and_globals/variables/#defining-collection-variables

@m3dbedb
Copy link

m3dbedb commented Nov 20, 2018

It's not stated crear, may I ask you to change the documentation, or where I may ask for it?

Note that defining a collection variable is a little different and can be done by editing the collection details.

Note that defining a collection variable is a little different and can be done by editing the collection details only.

@MikeNicholsPoints
Copy link

@m3dbedb My issue was unrelated. I had a pre-request script that was trying to generate a nonce token based on the body of the POST, but it was hanging when trying to deal with the collection variables. It was user error.

@sivcan
Copy link
Member

sivcan commented Nov 22, 2018

@m3dbedb Yes, we're currently actively working on improving our docs around this and I'd appreciate any feedback you have for us!

We already have a ticket around improving docs for variables which you can follow here: https://github.com/postmanlabs/postman-docs/issues/1442

Thanks!

@TheOneDaveYoung
Copy link

Why are collection variables read-only? Let's say I have a collection of related API calls that all need an auth token. I have one API call that collections a new auth token and a test script that grabs that value and stores it (globally or in the environment).

That's a problem for a couple of reasons. First, saving it globally is not good because often times variables like auth tokens will end up using the same name. Plus saving it globally kills the connection to the collection of related API calls.

The second issue, if saving to an environment, is that I have to remember which environment goes with which collection and I have to manually select the environment each time I switch collections and workspaces.

So, a couple of humble suggestions.

  • Make collection variables read/write
  • Add the ability to attach an environment to a collection

Thanks so much!

@nestorvanz
Copy link

I have the next issue for collection variables:

  1. Send request for generate token.
  2. Read response from request and save token into token. (To be used in other request)
  3. Send request using token generated in the past request. (Not working, the value is missing)
pm.variables.set("token", pm.response.json().token);
console.log(pm.variables.get("token"));

@m3dbedb
Copy link

m3dbedb commented Mar 14, 2019

Here is stated that pm.variables.set("token", pm.response.json().token); defines local variable, console.log(pm.variables.get("token")); get first existnig variable according to priority https://medium.com/@vdespa/demystifying-postman-variables-how-and-when-to-use-different-variable-scopes-66ad8dc11200

@alanamircruz
Copy link

In my case I just forgot to select my defined Environment (Development) on the Collection Runner, now I can see the variables working ok inside the tests. If it is useful for someone out there. Anyway, regards.

image

@axa88
Copy link

axa88 commented Jun 25, 2019

Why are collection variables read-only? Let's say I have a collection of related API calls that all need an auth token. I have one API call that collections a new auth token and a test script that grabs that value and stores it (globally or in the environment).

That's a problem for a couple of reasons. First, saving it globally is not good because often times variables like auth tokens will end up using the same name. Plus saving it globally kills the connection to the collection of related API calls.

The second issue, if saving to an environment, is that I have to remember which environment goes with which collection and I have to manually select the environment each time I switch collections and workspaces.

So, a couple of humble suggestions.

* Make collection variables read/write

* Add the ability to attach an environment to a collection

Thanks so much!

This should be made its own new issue. I kept trying to change a collection variable until i thought postman was flaky, uninstalled and just wrote an application.

@rcdailey
Copy link

rcdailey commented Jul 1, 2019

I'm still seeing this issue on v7.2.2. This isn't fixed at all. Unless I'm missing something, but this should be really straightforward. I define a variable called url in my collection. I do {{url}}/my/api/call in the URL field for my request. The {{url}} shows as unresolved by Postman.

@alanamircruz
Copy link

alanamircruz commented Jul 1, 2019 via email

@rcdailey
Copy link

rcdailey commented Jul 1, 2019

What do you mean "removing/refreshing environment variables"? These are collection variables, I do not have an environment set. I'm new to Postman so if I'm missing something please let me know.

@alanamircruz
Copy link

alanamircruz commented Jul 1, 2019 via email

@rcdailey
Copy link

rcdailey commented Jul 1, 2019

I have already read that article. Again I think this should be pretty straightforward. Variables work if I set them in an environment. But if I set the environment to empty/none and then set them in the Collection, it doesn't resolve the variables.

@ajdriver
Copy link

I'm still seeing this issue on v7.2.2. This isn't fixed at all. Unless I'm missing something, but this should be really straightforward. I define a variable called url in my collection. I do {{url}}/my/api/call in the URL field for my request. The {{url}} shows as unresolved by Postman.

@rcdailey I had the same issue 7.3.1., was a simple case of not saving the request. Once I saved it, the collection variables were recognised

@arrk-shch
Copy link

I am using v7.3.3. I am facing same issue. Not able to set "token" variable in collection. It works in environment and global variable.

@coditva
Copy link
Member

coditva commented Jul 19, 2019

Hey @rcdailey @arrk-shch

Unfortunately, I have been unable to reproduce this issue. Can you provide me with some more info:

  1. Is the request in which you're accessing the collection variable saved in the same collection? (It should be listed in the sidebar under the name of the collection).
  2. Does the issue still persist after closing the request tab and re-opening it from the Collections sidebar?
  3. If there is an environment variable of the same name as the collection variable? (Environment variables take precedence over collection variables. Ref: https://learning.getpostman.com/docs/postman/environments_and_globals/variables/#variable-scopes)

Also, if you can relate your steps to reproduce this issue, it would really help accelerate the process.

@arrk-shch
Copy link

Thanks @coditva for quick response.
I am trying simple scenario in postman. I have created a "token" variable in collection. First, I am executing Login API where I get the token in the header. This received token, I am trying to set it in collection variable. And then using the collection variable "token" in my next script under authentication tab as BearerToken selected in Type dropdown box and {["token"}} in Token textbox. But its not working as the variable is not getting set in collection.
Here is reply to you points

  1. Yes
  2. It simply doesn't get set. Tried restarting the postman as well.
  3. There is no duplicate variable in my environment, global & collection variable. When I hover the {{token}} then it shows me the scope of the variable as collection
    Let me know if you need more info.
    Thanks

@coditva
Copy link
Member

coditva commented Jul 19, 2019

@arrk-shch I see. The problem lies in the fact that Postman does not allow collection variables to be set with scripts.
Ref: https://learning.getpostman.com/docs/postman/environments_and_globals/variables/#accessing-variables-through-scripts

A feature request to allow this can be found here: #5053.

A quick solution would be to remove the token from the collection variables and set it as a global/environment variable.

@tomshy
Copy link

tomshy commented Sep 4, 2019

Not sure if this is solved. I realize this doesn't appear correctly in the UI, but it also doesn't work with shared collections or shared documentation

having same issue? has it been solved?

@PeterStaev
Copy link

PeterStaev commented Nov 8, 2019

I'm also experiencing this problem. I have defined variables in the collection. Not even modifying in Tests. And they are not recognized nor they are sent correctly. Here is a sample collection json:

{
  "info": {
    "_postman_id": "5e697462-e318-411a-b0e0-71174f51365e",
    "name": "Sample",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "item": [
    {
      "name": "Test",
      "event": [
        {
          "listen": "prerequest",
          "script": {
            "id": "fc300f35-f3de-433d-be6d-72ac086a6335",
            "exec": [
              "console.log(pm.collectionVariables.get(\"SomeVar\"))"
            ],
            "type": "text/javascript"
          }
        }
      ],
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "Var",
            "value": "{{ SomeVar }}",
            "type": "text"
          }
        ],
        "url": {
          "raw": "https://localhost/test?var={{ SomeVar }}",
          "protocol": "https",
          "host": [
            "localhost"
          ],
          "path": [
            "test"
          ],
          "query": [
            {
              "key": "var",
              "value": "{{ SomeVar }}"
            }
          ]
        }
      },
      "response": []
    }
  ],
  "variable": [
    {
      "id": "73a9a812-d9a4-4e85-aeae-450f66e8cee2",
      "key": "SomeVar",
      "value": "Test",
      "type": "string"
    }
  ],
  "protocolProfileBehavior": {}
}

EDIT
Fixed. Problem was that I had spaces around the variable...

@MuhammadimYusoff
Copy link

MuhammadimYusoff commented Feb 4, 2020

I have the same problem and this is year 2020 guys, this issue not fixed on macOS release v7.

*Workaround at the end of this post

Below is my version screenshot if you wanted:-
Screenshot 2020-02-04 at 3 28 38 PM

My problem goes back beyond 2018 it seems because someone appoint me the issue to this tread, "Unresolved Variable (not environment)".

  1. I created my variable in my collection, updated or saved it:

Screenshot 2020-02-04 at 3 28 15 PM

  1. I tried to use it after I have update it but "Unresolved Variables" appear:

Screenshot 2020-02-04 at 3 31 06 PM

Workaround by @adenix:

I have to save the request in the collection, close the request, and then re-open it.

But cant Postman just 'Update' it and Users can use it on-the-fly?
Kinda tedious to closed a tab about 10 times and re-open it again for a new variable added.

@wildfrontend
Copy link

maybe some issue just remove empty word could be work
check whether the variable have Enter keyword behind

截圖 2020-05-13 下午11 15 54

@kidstell
Copy link

v7.29.0 collection variables not working. It picks environment variables correctly but it wont even detect the collection variables.

@coditva
Copy link
Member

coditva commented Jul 24, 2020

@kidstell I haven't been able to reproduce this. Can you elaborate on the exact steps you took (e.g. create collection, go to edit collection, add variable, etc.)? It would also help if you can tell us the expected outcome and actual outcome that you saw.

@willbt
Copy link

willbt commented Jul 29, 2020

v7.29.0 collection variables not working. It picks environment variables correctly but it wont even detect the collection variables.

I am seeing the same thing. Collection Variables set in the Tests as pm.collectionVariables.set("key", "value") are null when you reference them as {{key}} in the headers or other tests.

@kidstell
Copy link

kidstell commented Jul 29, 2020 via email

@jagruuti
Copy link

I have defined a variable as a global variable and trying to use it in another request. I'm getting the same error as below:
image
Version used v8.1.0

@jagruuti
Copy link

I have defined a variable as a global variable and trying to use it in another request. I'm getting the same error as below:
image
Version used v8.1.0

It is working now. Strange!

@j1mmie
Copy link

j1mmie commented Mar 27, 2023

For any Googlers who arrive here - make sure you click the Save button after adding / changing variables.

Variables will not be recognized until they are saved. This is true both for Collections AND Environments.

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

No branches or pull requests