17
Views
5
Comments
Solved
REST Api error handling.
Question

Hi,

I am consuming REST API in Reactive web application and this API will only return data If I send security token(receiving it from different API call and storing it in a client variable) in the body of the request. This security token expires after 20 minutes, so I have to get a new token to get the data otherwise it will return 401 response.

Not sure how should I approach this.

Thank you for your time.


Rank: #68
Solution

Hi Nannu,


Usually tokens come also with an expiring date time. When you save the token you should also save that expiring date time and before you call you add an if to check if the token has expired. If so, you ask a new one, save the new one and the new expiring date and proceed. Otherwise you use the token as you know it is valid.

In any case you can have an exception handler allowing your action to fail gracefully.


Hope it helps.


Cheers,

João

Rank: #1613

João Marques wrote:

Hi Nannu,


Usually tokens come also with an expiring date time. When you save the token you should also save that expiring date time and before you call you add an if to check if the token has expired. If so, you ask a new one, save the new one and the new expiring date and proceed. Otherwise you use the token as you know it is valid.

In any case you can have an exception handler allowing your action to fail gracefully.


Hope it helps.


Cheers,

João

 

 Thank you so much, it worked for me.

Rank: #510

Hi Nannu,

How about calling both APIs in the same action flow and passing the token directly? Unless there are performance concerns with multiple calls in which case you probably need to store the token and expiry locally or in DB and perform a check each time.

Hi Nannu,


I have a similar situation in one of my applications too:

Our approach is proactively to check if the token expired before calling the API.

Like João Marques said:
"When you save the token, you should also save that expiring date time and before you call you to add an if to check if the token has expired."


In my case is JWT token, I used the Action ReadAndValidateToken from the component in Forge.


How to check the expired token?

DiffSeconds(CurrDateTime(), ReadAndValidateTokenWithJsonWebKey.Expiration) < Site.AuthTokenExpirationSpan

I added 5 seconds interval (Site.AuthTokenExpirationSpan) before expiring:

If the token is expired, I will refresh the token. ( you don't need to use the Exception to refresh the token).


Cheers,

Luís

Hi Nannu,

I also faced same issue I checked  check if the token expired before calling the API then get the new token before calling the API.


Regards

Shashikant Shukla