Skip to content

Instantly share code, notes, and snippets.

@krnbr
Last active October 17, 2019 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krnbr/bf818b8ea3fa016fd6f2471d946331e9 to your computer and use it in GitHub Desktop.
Save krnbr/bf818b8ea3fa016fd6f2471d946331e9 to your computer and use it in GitHub Desktop.
Postman's Pre-request Script
/*
NOTES
replace your http://your.oauth.server.domain/oauth/token with actual token url for your oauth server
replace your_client_id with actual client id of your oauth server
replace your_client_secret with actual client secret of your oauth server
replace "your scopes space seperated" with actual scopes
*/
const postRequest = {
url: "http://your.oauth.server.domain/oauth/token",
method: 'POST',
header: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
},
body: {
mode: 'urlencoded',
urlencoded: [
{key: "grant_type", value: "client_credentials", disabled: false},
{key: "client_id", value: "your_client_id", disabled: false},
{key: "client_secret", value: "your_client_secret", disabled: false},
{key: "scope", value: "your scopes space seperated", disabled:false}
]
}
};
console.log("start pre request script for your oauth protected API");
// uncomment for debug purpose
//console.log(JSON.stringify(postRequest));
pm.sendRequest(postRequest, function (err, response) {
var responseData = response.json();
// uncomment for debug purpose
//console.log("access_token is ---> "+responseData["access_token"]);
pm.environment.set("access_token",responseData["access_token"]);
});
console.log("end pre request script for your oauth protected API");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment