Skip to content
Discussion options

You must be logged in to vote

Eventually I understood the whole approach was wrong. The token status is best checked before making a request.

Here is a working collection pre-request script.

const moment = require('moment');

const exp =  bru.getVar('token_expiration_ts')

if (!exp || moment().isAfter(exp)) {
  console.debug('token expired or missing, refreshing...')

  const loginRes = await bru.sendRequest({
      method: "POST",
      url: `${bru.getEnvVar('BASE_URL')}/login`,
      data: {
          credential: bru.getEnvVar('LOGIN_CREDENTIALS_BASE64')
      }
  });
  
  bru.setVar('token', loginRes.data.token)
  bru.setVar('token_expiration_ts', moment().add(loginRes.data.expires_in, 'seconds').format() )
}

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by maxiride
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant