# Token renewall function to be run only one time during execution

**URL:** <https://community.gatling.io/t/token-renewall-function-to-be-run-only-one-time-during-execution/8410>\
**Category:** Gatling (Open-Source)\
**Created:** [October 30, 2023, 3:55pm UTC](https://community.gatling.io/t/token-renewall-function-to-be-run-only-one-time-during-execution/8410 "2023-10-30T15:55:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![boxfreeman](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@boxfreeman](https://community.gatling.io/u/boxfreeman)\
**Post date:** [October 30, 2023, 3:55pm UTC](https://community.gatling.io/t/token-renewall-function-to-be-run-only-one-time-during-execution/8410/1 "2023-10-30T15:55:35Z")

</div>

Hello everyone. How are you?

I have a problem with my Gatling execution. We have a specific way of generating tokens for the user session. We first call a function outside the Gatling scenario to set up the Authorization header, and then I start the execution. The problem is that the token only lasts 10 minutes and I want to generate a new one during the execution.

I’ve tried multiple approaches, like calling the token function when I receive the 401 status code, but I always end up with the token being generated for all the 100 users, when instead I just want to run the function only ONE time.

Can anyone help me with that?

---

<div class="post-metadata">

**Author:** ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)\
**Post date:** [October 30, 2023, 4:14pm UTC](https://community.gatling.io/t/token-renewall-function-to-be-run-only-one-time-during-execution/8410/2 "2023-10-30T16:14:21Z")

</div>

Hi @boxfreeman,

Welcome aboard!

@GeMi already created a full example for your use case:

> [@Token Refresh - Java - Example](https://community.gatling.io/t/token-refresh-java-example/7935):
>
> I see that lot of people asking about solution to refresh Token when expired. I have prepared Case0022SetOrRefreshTokenSimulation where Token is refreshed every some period of time. What you think about this solution @slandelle @sbrevet ?

Enjoy!

Cheers!

---

<div class="post-metadata">

**Author:** ![boxfreeman](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@boxfreeman](https://community.gatling.io/u/boxfreeman)\
**Post date:** [October 31, 2023, 12:46pm UTC](https://community.gatling.io/t/token-renewall-function-to-be-run-only-one-time-during-execution/8410/3 "2023-10-31T12:46:04Z")

</div>

Hello @sbrevet  
Thank you very much! It worked perfectly here!  
One thing is that my console output now has the other scenarios exits.  
I was wondering if there is any way to hide the output of the scenario on the console or in the final metrics.  
If there is no way to hide it, no worries.  
Thanks again!!

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [November 5, 2023, 10:07am UTC](https://community.gatling.io/t/token-renewall-function-to-be-run-only-one-time-during-execution/8410/4 "2023-11-05T10:07:43Z")

</div>

> **[HTTP Request](https://gatling.io/docs/gatling/reference/current/http/request/#silent)**
>
> Create HTTP requests

---

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/b/b148e0742de35c6280726cabf3ecd3de2d320f63.png) [@system](https://community.gatling.io/u/system)\
**Post date:** [December 5, 2023, 10:08am UTC](https://community.gatling.io/t/token-renewall-function-to-be-run-only-one-time-during-execution/8410/5 "2023-12-05T10:08:02Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
