5

I have variable "TOKEN" in my collection scope. I try to set the value using Tests scripts when do a request. But the variable not changed.

So, I try to use environment scope variable. And It works.

Why It's doesn't work when in collection scope? I had read about postman variable scope here and understand it well.

Here're some screenshots:

1. First, I call login endpoint.

Call Login

Below is the console result. Nothing wrong.

Console Result

Until I try to get all users endpoint that required token in request header. The status is 401 because the token is null. If the token is not null, then it will return 200:

Get Users Endpoint

It's gracefully working when I add "TOKEN" variable to environment. Switch to No Environment again will result 401 status code:

Result

Ivan Elianto
  • 432
  • 5
  • 19
  • 2
    Please add the code you're using and some screen shots, to help explain what you're doing. Currently, it says "I have this thing I'm not showing you and I have this code doing something you can't see" :) You need to provide more context for people to help. – Danny Dainton May 05 '19 at 10:07
  • Okay, I had add it recently. @DannyDainton – Ivan Elianto May 05 '19 at 10:27
  • 2
    Are you trying to programmatically set the variable at the collection level? You will only be able to read or get those and not write or set them. – Danny Dainton May 05 '19 at 18:42
  • @DannyDainton Okay. I think it's good to explain in postman's page about this. – Ivan Elianto May 06 '19 at 10:35
  • Does this answer your question? [Accessing Collection Variables in Postman](https://stackoverflow.com/questions/47680580/accessing-collection-variables-in-postman) – Henke Jan 26 '21 at 10:51
  • It took some time, but I have now written the answer I set out to do - https://stackoverflow.com/a/65942023. – Henke Jan 28 '21 at 17:17

4 Answers4

3

This may be new since this question was posted, but for anyone else finding this, you can set collection variables using:

pm.collectionVariables.set(key, value)

See: https://learning.postman.com/docs/sending-requests/variables/#defining-variables-in-scripts

Henke
  • 4,445
  • 3
  • 31
  • 44
1

You can now use:

pm.collectionVariables.set("variable_key", "variable_value");
  • In my original answer in June 2019, I wrote that collection variables are not editable through scripts, and can only be changed manually. As noted, this is no longer the case. . . .*
Decypher
  • 690
  • 1
  • 8
  • 30
  • @Henke https://assets.postman.com/postman-docs/Variables-Chart.png https://learning.postman.com/docs/sending-requests/variables/ This is what it says on the postman docs, if it changed over the years then maybe they added a new way of doing it. But this was the status of june 2019 You now can indeed use: pm.collectionVariables.set("variable_key", "variable_value"); – Decypher Jan 26 '21 at 18:18
  • Since you have updated your answer, I removed my two comments which did not make much sense any more. In your current answer, you might want to replace everything below the horizontal line with something like: _In my original answer in June 2019, I wrote that collection variables are not editable through scripts, and can only be changed manually. As noted, this is no longer the case._ . . . You might also want to take a look at https://meta.stackexchange.com/a/127655 and https://meta.stackoverflow.com/a/255685 Cheers! – Henke Jan 27 '21 at 09:26
0

Turn off Automatic persist variable values from postman settings.

image

K14
  • 181
  • 7
  • This is not exactly an anwer to the question but was useful for me anyway. :) This solves the problem with `pm.environment.set` setting both `current` and `initial` variable values. – Bohdan Ganicky Feb 07 '20 at 17:29
0

It seems you can only set env variables when some environment is selected: https://learning.postman.com/docs/postman/scripts/postman-sandbox/#environment-and-global-variables

enter image description here

Bohdan Ganicky
  • 159
  • 1
  • 6