Created
May 13, 2019 08:21
-
-
Save code-atom/54631bd8b72de81d6a0053ae26b19c73 to your computer and use it in GitHub Desktop.
Add pre-request script for postman
This file contains hidden or 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
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