You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Get postman to automatically generate an access token and set up your host and make any request
Changes based on request and response
Change host to your 'host'
Your access token key in response maybe different could vary from being acessToken, token, access_token and so on, read accordingly and change the conde where ever access_toekn is being called
Same for expires in
pm.collectionVariables.set("host", "https://reqbin.com/echo/post/json");
const authTokenReq = {
url: 'https://reqbin.com/echo/post/json',
method: 'POST',
header: {
"content-type": "application/json"
},
body: {
mode: 'raw',
raw: JSON.stringify({
"username": "your user name",
"password": "your password and other key valus pairs if needed"
})
}
};
const porcessResponse = (err, response) => {
pm.collectionVariables.set("access_token", response.json().access_token);
var expiryDate = new Date();
expiryDate.setSeconds(expiryDate.getSeconds() + response.json().expires_in);
pm.collectionVariables.set("expires_in", expiryDate.getTime());
};
if (!pm.collectionVariables.get('expires_in') || !pm.collectionVariables.get('access_token')) {
pm.sendRequest(authTokenReq, (err, response) => porcessResponse(err, response));
} else if (pm.collectionVariables.get('expires_in') <= (new Date()).getTime()) {
pm.sendRequest(authTokenReq, (err, response) => porcessResponse(err, response));
}