Skip to content

Instantly share code, notes, and snippets.

@code-atom
Created May 13, 2019 08:21
Show Gist options
  • Save code-atom/54631bd8b72de81d6a0053ae26b19c73 to your computer and use it in GitHub Desktop.
Save code-atom/54631bd8b72de81d6a0053ae26b19c73 to your computer and use it in GitHub Desktop.
Add pre-request script for postman
const stsEndpoint = pm.variables.get("STSEndpoint");
const clientCredentialBase64Encode = pm.variables.get("clientSecret64Encode");
const scopes = pm.variables.get("scopes");
let getToken = true;
const tokenEndpoint = stsEndpoint + '/connect/token';
if (!pm.variables.get('token')) {
console.log('Token missing');
} else {
getToken = false;
console.log('Token all good');
}
const echoPostRequest = {
url: tokenEndpoint,
method: 'POST',
header: {
'Authorization': clientCredentialBase64Encode,
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
},
body:{
mode: 'urlencoded',
urlencoded: [
{key: "grant_type", value: "client_credentials", disabled: false},
{key: "scope", value: scopes, disabled: false}
]
}
};
if (getToken === true) {
pm.sendRequest(echoPostRequest, function (err, res) {
if (err === null) {
var responseJson = res.json();
pm.variables.set('token', responseJson.access_token);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment