-
👉 Skip to #5523 (comment) for an updated situation I am interacting with a non-standard API, the POST I am trying to implement a retry mechanism for when the token expires, so that I don't have to manually update it. I've tried to implement a collection post request script but when the token expires I am still receiving a 401. Through the console I am not understanding if the request fails because:
What I have done so far
![]()
// if original request fails with 401, perform a token refresh and retry the original request
if (res.status === 401) {
console.log('token expired, attempting refresh...')
const loginRes = await bru.sendRequest({
method: "POST",
url: `${bru.getEnvVar('BASE_URL')}/login`,
data: {
credential: bru.getEnvVar('LOGIN_CREDENTIALS_BASE64')
}
});
// login request succeded
if (loginRes.status === 200) {
bru.setVar('token', loginRes.data.token)
console.log('token updated')
bru.setNextRequest(req.getName())
}
} I will gladly appreciate any possible guidance, because other than fixing this specific issue I feel like I can't troubleshoot the issue at all which is more important than the actual solution. Edits:
if (loginRes.status === 200) {
/.../
console.debug('original req', req)
req.setHeader('Authorization', `Bearer ${bru.getVar('token')}`)
console.debug('updated req', req)
const retryRes = await bru.sendRequest(req)
console.log('retryRes', retryRes)
/.../ |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
After a lot fiddling I managed to update the original request headers and execute again the original request with the updated auth header. The new request is executed correctly and the state of However, I just found out that the Collection Post response script // if original request fails with 401, perform a token refresh and retry the original request
if (res.status === 401) {
console.log('token expired, refresh...')
const loginRes = await bru.sendRequest({
method: "POST",
url: `${bru.getEnvVar('BASE_URL')}/login`,
data: {
credential: bru.getEnvVar('LOGIN_CREDENTIALS_BASE64')
}
});
if (loginRes.status === 200) {
bru.setVar('token', loginRes.data.token)
console.log('token updated')
req.headers = {
...req.headers, // re-apply original headers
'Authorization': `Bearer ${bru.getVar('token')}`, // any field defined after then expansion ...req.headers overwrites previously defined fields
}
const retryRes = await bru.sendRequest(req)
console.log('retryRes', retryRes)
res = retryRes // res read-only
}
} |
Beta Was this translation helpful? Give feedback.
-
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() )
} |
Beta Was this translation helpful? Give feedback.
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.