# How to refresh JWT token

**URL:** <https://community.gatling.io/t/how-to-refresh-jwt-token/9145>\
**Category:** Gatling (Open-Source)\
**Created:** [July 8, 2024, 12:38pm UTC](https://community.gatling.io/t/how-to-refresh-jwt-token/9145 "2024-07-08T12:38:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![markus](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@markus](https://community.gatling.io/u/markus)\
**Post date:** [July 8, 2024, 12:38pm UTC](https://community.gatling.io/t/how-to-refresh-jwt-token/9145/1 "2024-07-08T12:38:15Z")

</div>

**Gatling version** : 3.11.5  
**Gatling flavor** : java kotlin scala javascript typescript  
**Gatling build tool** : maven gradle sbt bundle npm

Hi,

I have a question about a _simple_ simulation with Gatling.

Before a request will be send to a server an JWT token must be fetched and before five minutes the token should be refreshed.

```auto
    private final ScenarioBuilder scenario = scenario("Fetch order")
            .exec(
                    http("Authenticate")
                            .post(AUTHENTICATION_URL)
                            .asFormUrlEncoded()
                            .formParam("username", "username")
                            .formParam("password", "password")
                            .check(status().is(OK.code()))
                            .check(jsonPath("$.token").saveAs("token"))
            )
            .feed(feeder)
            .tryMax(3).on(
                    exec(
                            http("Fetch order")
                                    .get("#{auftragId}")
                                    .header(AUTHORIZATION, "Bearer #{token}")
                                    .header(ACCEPT, "application/json")
                    )
            ); private final ScenarioBuilder scenario = scenario("Fetch order")
            .exec(
                    http("Authenticate")
                            .post(AUTHENTICATION_URL)
                            .asFormUrlEncoded()
                            .formParam("username", "username")
                            .formParam("password", "password")
                            .check(status().is(OK.code()))
                            .check(jsonPath("$.token").saveAs("token"))
            )
            .feed(feeder)
            .tryMax(3).on(
                    exec(
                            http("Fetch order")
                                    .get("#{auftragId}")
                                    .header(AUTHORIZATION, "Bearer #{token}")
                                    .header(ACCEPT, "application/json")
                    )
            );

```

Now I spend my time to find out how to refresh the token efficently. But I did not found a solution.

Some sources I read was:

> <https://stackoverflow.com/questions/70886918/refresh-bearer-token-during-gatling-load-tests>

> <https://github.com/gemiusz/gatling-examples-maven-java/blob/master/src/test/java/pl/gemiusz/Case0022SetOrRefreshTokenSimulation.java>

But at the end I didn’t get it.

```auto
    private final HttpProtocolBuilder httpProtocol = http
            .baseUrl(BASE_URL);
    {
        this.setUp(scenario.injectOpen(atOnceUsers(1))).protocols(httpProtocol);
    }

```

How to get a Gatling simulation to run where I have to create/refresh a JWT token and query a server for some data?

Thanks in advance,  
Markus

---

<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:** [July 8, 2024, 12:53pm UTC](https://community.gatling.io/t/how-to-refresh-jwt-token/9145/2 "2024-07-08T12:53:20Z")

</div>

Hi @markus

What did you implement?

I remember reviewing @GeMi’s solution and it was completely working.

Take care:

- @GeMi’s solution is about a sharing token for all users
- @GeMi’s solution contains an injection profile with different scenarios.

You may want to check if the time has come to refresh the token at every step in your scenario.  
In that case, you will have to code an helper that surround a step with your verification (a comparison of time versus a value in the session) then perform refresh before the actual call to your step.

Does that help?

Cheers!

---

<div class="post-metadata">

**Author:** ![markus](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@markus](https://community.gatling.io/u/markus)\
**Post date:** [July 8, 2024, 1:45pm UTC](https://community.gatling.io/t/how-to-refresh-jwt-token/9145/3 "2024-07-08T13:45:51Z")

</div>

Hi @sbrevet,

I let the example of @GeMi run on my machine and it works.

But where can I inject my code:

```auto

                  exec(
                            http("Fetch order")
                                    .get("#{auftragId}")
                                    .header(AUTHORIZATION, "Bearer #{token}")
                                    .header(ACCEPT, "application/json")
                    )

```

I see a scenario for creating the token (scnForSetFirstToken), I see the scnForRefreshToken stuff and how the token will be abort (scnForRefreshTokenAbort)

```auto
    {
        setUp(scnForSetFirstToken.injectOpen(atOnceUsers(1))
                .andThen(
                        scnForRefreshToken.injectOpen(atOnceUsers(1)),
                        scn.injectOpen(constantUsersPerSec(10).during(30))
                                .andThen(scnForRefreshTokenAbort.injectOpen(atOnceUsers(1)))
                )
        ).protocols(httpProtocol);
    }

```

But now I see where the scenario runs “scn”. I will rewrite it to my purpose and hope it will work.

I can rely on the fact that the scnForRefreshToken is working on the background.  
In my understanding there are two scenarios are running to the same time?

Thanks a lot. I should got it.

Cheers,  
Markus

---

<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:** [August 7, 2024, 1:46pm UTC](https://community.gatling.io/t/how-to-refresh-jwt-token/9145/4 "2024-08-07T13:46:15Z")

</div>

Closed on August 7, 2024, 1:46pm UTC

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