# Environment variable not updated from authentication response

**URL:** <https://community.postman.com/t/environment-variable-not-updated-from-authentication-response/19557>\
**Category:** Help Hub\
**Tags:** variables, authentication, json\
**Created:** [January 26, 2021, 9:12am UTC](https://community.postman.com/t/environment-variable-not-updated-from-authentication-response/19557 "2021-01-26T09:12:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![danethorson](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danethorson/32/7090_2.png) [@danethorson](https://community.postman.com/u/danethorson)\
**Post date:** [January 26, 2021, 9:12am UTC](https://community.postman.com/t/environment-variable-not-updated-from-authentication-response/19557/1 "2021-01-26T09:12:08Z")

</div>

Hello all.

I have been trying everything i could think of, but variable is not being updated from the response. I have a feeling that i have missed some small detail but i just can’t put my finger on it.

```
  // Getting auth token from Keycloak
pm.sendRequest({ 
    url:`https://${pm.globals.get("KC_HOST")}:${pm.globals.get("KC_PORT")}/auth/realms/${pm.globals.get("KC_REALM")}/protocol/openid-connect/token`, 
        method:"POST",
        header: {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Basic " + btoa("realm_user:password")
        },
        body: {
            mode: "urlencoded",
            urlencoded: [
                { key: "grant_type", value: "password" },
                { key: "username", value: "username" },
                { key: "password", value: "password" },
            ]
        },
    // setting response to variable
        function (err, response) {
            if (err === null) {
            pm.environment.set("access_token",response.json());
            }
        }
    }),

```

regardless what i try - access\_token environment variable stays empty.  
Keycloak returns a JSON with access token, beginning with:  
`{"access_token":"key", ...}`  
i wanted to retrieve value of access\_token, store it in variable and use it in the next request. But no, variable always comes up empty.  
What i am doing wrong here?

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [January 26, 2021, 9:31am UTC](https://community.postman.com/t/environment-variable-not-updated-from-authentication-response/19557/2 "2021-01-26T09:31:11Z")

</div>

Hello @danethorson , Just by doing a quick eye-balling I am seeing that it should

“pm.response.json()”

```auto
var resp = pm.response.json();
postman.setEnvironmentVariable("access_token", resp.access_token);

```

May be you can try assigning the response and try to reach the token in the response.

I hope this helps 🙂 Please let me know.

---

<div class="post-metadata">

**Author:** ![danethorson](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danethorson/32/7090_2.png) [@danethorson](https://community.postman.com/u/danethorson)\
**Post date:** [January 26, 2021, 9:57am UTC](https://community.postman.com/t/environment-variable-not-updated-from-authentication-response/19557/3 "2021-01-26T09:57:00Z")

</div>

Thanks @bpricilla 🙂  
I tied to set it like you described:

```
function (err, response) {
        if (err === null) {
            var resp = pm.response.json();
            postman.setEnvironmentVariable("access_token",resp.access_token);
        }
    }

```

but result is the same - no value is retrieved ☹

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [January 26, 2021, 9:59am UTC](https://community.postman.com/t/environment-variable-not-updated-from-authentication-response/19557/4 "2021-01-26T09:59:59Z")

</div>

@danethorson Can you try the below one please

```auto
var resp = pm.response.json();

if (resp.access_token) {
  postman.setEnvironmentVariable("access_token", resp.access_token);
}

```

---

<div class="post-metadata">

**Author:** ![danethorson](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danethorson/32/7090_2.png) [@danethorson](https://community.postman.com/u/danethorson)\
**Post date:** [January 26, 2021, 10:04am UTC](https://community.postman.com/t/environment-variable-not-updated-from-authentication-response/19557/5 "2021-01-26T10:04:10Z")

</div>

> [@bpricilla](#):
>
> `if (resp.access_token)`

Same. Variable is empty ☹ I will turn 🤢 soon, from trying to figure out what i am missing here

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [January 26, 2021, 10:17am UTC](https://community.postman.com/t/environment-variable-not-updated-from-authentication-response/19557/6 "2021-01-26T10:17:36Z")

</div>

Oh @danethorson I understand that. Can you share the screenshot possibly? It should be simple, we are missing some minute detail that’s it 😊

```auto
function (err, res) {
        pm.environment.set("access_token", res.json().access_token);
  });

```

Do you mind trying the above snippet?

---

<div class="post-metadata">

**Author:** ![danethorson](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danethorson/32/7090_2.png) [@danethorson](https://community.postman.com/u/danethorson)\
**Post date:** [January 26, 2021, 10:31am UTC](https://community.postman.com/t/environment-variable-not-updated-from-authentication-response/19557/7 "2021-01-26T10:31:14Z")

</div>

> [@bpricilla](#):
>
> ```auto
> function (err, res) {
> pm.environment.set("access_token", res.json().access_token);
> 
> ```

the same ☹  
This is environment variable setup for this environment. the rest are taken from global ones.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/5/e/5e7cd1d25bd7ec64d06521c49ce207219fc73cba.png)  
Keycloak response body:  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/b/4/b46ec4749e7c2742ccefd450d545f1f50f081f62.png)  
Here i am trying to pass it to the next request:  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/2/a21c1bdc8b38ffe293f1a7d7f8a3a7f5c8ca23a2.png)  
empty.  
I also added  
console.log(pm.environment.get(“access\_token”)),  
to double check - it’s value. Zero ☹

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [January 26, 2021, 11:47am UTC](https://community.postman.com/t/environment-variable-not-updated-from-authentication-response/19557/8 "2021-01-26T11:47:39Z")

</div>

@danethorson looking at the access token value I remember this [blog](https://dannydainton.com/2018/09/10/postman-the-bearer-of-good-news/) by @danny-dainton . I strongly suggest you to read this 🙂 This can solve the problem!!

---

<div class="post-metadata">

**Author:** ![danethorson](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danethorson/32/7090_2.png) [@danethorson](https://community.postman.com/u/danethorson)\
**Post date:** [January 26, 2021, 2:45pm UTC](https://community.postman.com/t/environment-variable-not-updated-from-authentication-response/19557/9 "2021-01-26T14:45:24Z")

</div>

@bpricilla Thank you thank you thank you!!! Finally it worked!!! 🥰

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [January 26, 2021, 11:17pm UTC](https://community.postman.com/t/environment-variable-not-updated-from-authentication-response/19557/10 "2021-01-26T23:17:09Z")

</div>

oh that’s great @danethorson !! I hope you are relaxed now. Have a great day 😊
