3

I have a login request that returns a token that gets saved to environment variables via the following script added in Postman - Tests tab:

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("token_abc", jsonData.access_token);

Another request uses that token in the Authorization header as Bearer {{token_abc}}.

But, it seems the token is valid only for one-time use: after request #2 is sent, the value of token_abc is reset to null in Environment variables - Current value.

What could be causing this? Because this is not how the application usually behaves - the token should be valid for 24 hours. Is there a Postman setting I'm not seeing somewhere? I'm using Postman 8.6.1.

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
  • How do you run the requests? Collection runner, newman, or manually? I tried running a few requests manually after setting an env var like you do, and it was not deleted from env variables. I was not able to simulate your problem on my side, I use the same version of Postman. – pavelsaman Jun 09 '21 at 09:11
  • 1
    I run them manually. There are 'initial value' and 'current value' columns in environment variables. Inital value is persisted after 1 request, but the current vallue is set to null. – Mate Mrše Jun 09 '21 at 09:17
  • Can you post an image by editing the post of what you're seeing, trying to explain that is going to be a lot harder? The only way that the variable would be overridden like that is if, in Request #2, you have some code in the Pre-request or Test tab setting it. Did the first request return the correct response, what's the structure of that response? – Danny Dainton Jun 09 '21 at 09:26
  • @MateMrše: interesting. what I see is that initial value is never set and current value is correctly set and never reset to null. this is expected behaviour in my view, just no idea why it doesn't work on your side. – pavelsaman Jun 09 '21 at 09:35
  • You could create the same behaviour if you try and set something that is not part of the response - `pm.environment.set("test", pm.response.json().test)` would create a `test` variable and set that to `null`. I don't think it's the case here, it's more than likely something in the script overriding the variable. – Danny Dainton Jun 09 '21 at 09:38

2 Answers2

2

For some reason unknown to me there are 2 "postman" object in the postman sandbox that you can use to set environment variables.

  • postman
  • pm

Both can call a function for setting variables on the variable scopes in postman. From what I gathered on forums discussing various postman problems, the usage of the postman object gets discouraged in favor of pm.

See if this helps:

//check if you get the value you expected first
console.log(jsonData.access_token)
pm.environment.set('token_abc', jsonData.access_token)

Since your variable gets reset somewhere, there must be code in your script responsible for that. You mention the variable gets reset after request #2, so the first place to look is in the "Tests" tab of your second request. You can use console.log(value) to output the value of that variable to console on various places in that script in order to further pinpoint the exact location in code where it gets reset.

PixelPlex
  • 749
  • 5
  • 21
  • you did receive the expected output when you logged the value of your token? Do you have other script running e.g. on your collection folder that might clear that variable or otherwise interfere? You also mention it gets set to null after request #2, so perhaps some code on that request clears the variable? – PixelPlex Jun 09 '21 at 09:21
  • 2
    `postman.setEnvironmentVariable` is the older syntax that was super seeded a long time ago (around version 5) by the `pm.*` API which includes various other functions. – Danny Dainton Jun 09 '21 at 09:22
  • @PixelPlex Yes, that was the problem. Post processing scritpt on the second request overwrote the variable! If you will write that as the answer, I will accept it. Thanks! – Mate Mrše Jun 09 '21 at 09:33
  • @MateMrše I would strongly recommend adding visual examples when asking questions, someone would have pointed that out from an image and saved you time. – Danny Dainton Jun 09 '21 at 09:35
  • @DannyDainton is absolutely correct, if users have to start asking questions, your question might have needed some improvement. I'll adjust my answer to also include the commented information. – PixelPlex Jun 09 '21 at 09:41
1

Change the name of the variable. This happens if the name of the variable is bearerToken.

Kadri Umay
  • 11
  • 1