danethorson
(Marcis Buhholcs)
January 26, 2021, 9:12am
1
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?
bpricilla
(Pricilla B)
January 26, 2021, 9:31am
2
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 Please let me know.
1 Like
danethorson
(Marcis Buhholcs)
January 26, 2021, 9:57am
3
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
bpricilla
(Pricilla B)
January 26, 2021, 9:59am
4
@danethorson Can you try the below one please
var resp = pm.response.json();
if (resp.access_token) {
postman.setEnvironmentVariable("access_token", resp.access_token);
}
danethorson
(Marcis Buhholcs)
January 26, 2021, 10:04am
5
bpricilla:
if (resp.access_token)
Same. Variable is empty I will turn soon, from trying to figure out what i am missing here
bpricilla
(Pricilla B)
January 26, 2021, 10:17am
6
Oh @danethorson I understand that. Can you share the screenshot possibly? It should be simple, we are missing some minute detail that’s it
function (err, res) {
pm.environment.set("access_token", res.json().access_token);
});
Do you mind trying the above snippet?
danethorson
(Marcis Buhholcs)
January 26, 2021, 10:31am
7
the same
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
bpricilla
(Pricilla B)
January 26, 2021, 11:47am
8
@danethorson looking at the access token value I remember this blog by @danny-dainton . I strongly suggest you to read this This can solve the problem!!
1 Like
danethorson
(Marcis Buhholcs)
January 26, 2021, 2:45pm
9
@bpricilla Thank you thank you thank you!!! Finally it worked!!!
1 Like
bpricilla
(Pricilla B)
January 26, 2021, 11:17pm
10
oh that’s great @danethorson !! I hope you are relaxed now. Have a great day
1 Like