Created
March 11, 2024 12:01
-
-
Save DineshSolanki/2ca27fae969f10aec31de9325db9d62b to your computer and use it in GitHub Desktop.
generate and set new token variable if postman request fails with 401
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (pm.response.code === 401) { | |
pm.sendRequest({ | |
url: 'https://YOUR-KEYCLOAK-SERVER/realms/REALM-NAME/protocol/openid-connect/token', | |
method: 'POST', | |
header: 'Content-Type:application/x-www-form-urlencoded', | |
body: { | |
mode: 'urlencoded', | |
urlencoded: [ | |
{key: "grant_type", value: "client_credentials"}, | |
{key: "client_id", value: "YOUR CLIENT ID"}, | |
{key: "client_secret", value: "YOUR CLIENT SECRET"}, | |
{key: "scope", value: "openid"} | |
] | |
} | |
}, (err, res) => { | |
if (err) { | |
console.log(err); | |
} else { | |
pm.collectionVariables.set('bearerToken', res.json().access_token); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment