Skip to content

Instantly share code, notes, and snippets.

@DineshSolanki
Created March 11, 2024 12:01
Show Gist options
  • Save DineshSolanki/2ca27fae969f10aec31de9325db9d62b to your computer and use it in GitHub Desktop.
Save DineshSolanki/2ca27fae969f10aec31de9325db9d62b to your computer and use it in GitHub Desktop.
generate and set new token variable if postman request fails with 401
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