Environment variable not updated from authentication response

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?

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

“pm.response.json()”

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 :slight_smile: Please let me know.

1 Like

Thanks @bpricilla :slight_smile:
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 :frowning:

@danethorson Can you try the below one please

var resp = pm.response.json();


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

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

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

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

Do you mind trying the above snippet?

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


Keycloak response body:

Here i am trying to pass it to the next request:

empty.
I also added
console.log(pm.environment.get(“access_token”)),
to double check - it’s value. Zero :frowning:

@danethorson looking at the access token value I remember this blog by @dannydainton . I strongly suggest you to read this :slight_smile: This can solve the problem!!

1 Like

@bpricilla Thank you thank you thank you!!! Finally it worked!!! :smiling_face_with_three_hearts:

1 Like

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

1 Like