Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const echoPostRequest = {
- url: 'https://crm-api-dev.ntm.eu/api/v1/auth/ticket',
- method: 'POST',
- header: 'Content-Type:application/json',
- body: {
- mode: 'application/json',
- raw: JSON.stringify(
- {
- clientname:'<clientname>',
- username:'<username>',
- password:'<password>'
- })
- }
- };
- var getToken = true;
- if (!pm.environment.get('accessTokenExpiry') ||
- !pm.environment.get('currentAccessToken')) {
- console.log('Token or expiry date are missing')
- } else if (pm.environment.get('accessTokenExpiry') <= (new Date()).getTime()) {
- console.log('Token is expired')
- } else {
- getToken = false;
- console.log('Token and expiry date are all good');
- }
- if (getToken === true) {
- pm.sendRequest(echoPostRequest, function (err, res) {
- console.log(err ? err : res.json());
- if (err === null) {
- console.log('Saving the token and expiry date')
- var responseJson = res.json();
- pm.environment.set('currentAccessToken', responseJson.accessToken)
- var expiryDate = new Date();
- expiryDate.setSeconds(expiryDate.getSeconds() + responseJson.ttl);
- pm.environment.set('accessTokenExpiry', expiryDate.getTime());
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement