# Get auth token with one user from one url and use it with multiple users to test another url

**URL:** <https://community.gatling.io/t/get-auth-token-with-one-user-from-one-url-and-use-it-with-multiple-users-to-test-another-url/8816>\
**Category:** Gatling (Open-Source)\
**Created:** [March 17, 2024, 10:38pm UTC](https://community.gatling.io/t/get-auth-token-with-one-user-from-one-url-and-use-it-with-multiple-users-to-test-another-url/8816 "2024-03-17T22:38:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![amidavidw](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@amidavidw](https://community.gatling.io/u/amidavidw)\
**Post date:** [March 17, 2024, 10:38pm UTC](https://community.gatling.io/t/get-auth-token-with-one-user-from-one-url-and-use-it-with-multiple-users-to-test-another-url/8816/1 "2024-03-17T22:38:08Z")

</div>

Hi,

I’d like to use gatling for the following scenario:

1. Get the jwt token from one url, for example, [abc.com](http://abc.com), in one scenario
2. Use it in another scenario to test another url, for example [def.com](http://def.com), in another scenario

How to do that?

I tried as the code snippet below, but it does not work.  
Got the error:  
`'Get all books' failed to execute: No attribute named 'jwtToken' is defined`

```java
        List<String> jwt = new ArrayList<>(1);

        // Http Configuration
        HttpProtocolBuilder bookStore = http
                .baseUrl("https://abc.com/api")
                .acceptHeader("application/json").contentTypeHeader("application/json");

        HttpProtocolBuilder auth = http
                .baseUrl("https://def.com")
                .acceptHeader("application/json").contentTypeHeader("application/x-www-form-urlencoded");

        // HTTP CALLS
        ChainBuilder authenticate = exec(http("Authenticate")
                .post("/oauth/token")
                .formParam("client_id", "abc")
                .formParam("client_secret", "def")
                .formParam("grant_type", "client_credentials")
                .formParam("request_type", "token")
                .check(jmesPath("token").saveAs("jwtToken"))
                .check(status().is(200)))
                .exec(session -> {
                    jwt.add(session.getString("jwtToken"));
                    System.out.println("JWT: " + jwt);
                    return session;
                });

        ChainBuilder getAll = exec(
                http("Get all books")
                        .get("/books")
                        .header("Authorization", "Bearer #{jwtToken}"));

        ChainBuilder create = exec(http("Create book").post("/books")
                .header("Authorization", "Bearer #{jwtToken}")
                .body(ElFileBody("book.json")).asJson()
                .check(status().is(201)));

        ScenarioBuilder scnAuth = scenario("Auth")
                .exec(authenticate);

        ScenarioBuilder scn = scenario("Book Store Test")
                .exec(s -> {
                    System.out.println("JWT to use: " + jwt.get(0));
                    s.set("jwtToken", jwt.get(0));
                    return s;
                })
                .exec(getAll)
                .exec(create);

        setUp(scnAuth.injectOpen(atOnceUsers(1)).protocols(auth).andThen(List.of(
                scn.injectOpen(nothingFor(5), rampUsers(20).during(30))
                        .protocols(bookStore))));

```

---

<div class="post-metadata">

**Author:** ![trinp](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/trinp/32/1593_2.png) [@trinp](https://community.gatling.io/u/trinp)\
**Post date:** [March 18, 2024, 12:51am UTC](https://community.gatling.io/t/get-auth-token-with-one-user-from-one-url-and-use-it-with-multiple-users-to-test-another-url/8816/2 "2024-03-18T00:51:09Z")

</div>

There is an old question relating about this behavior, check it out:

> <https://stackoverflow.com/questions/28217667/share-data-between-gatling-scenarios>

---

<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:** [March 18, 2024, 7:42am UTC](https://community.gatling.io/t/get-auth-token-with-one-user-from-one-url-and-use-it-with-multiple-users-to-test-another-url/8816/3 "2024-03-18T07:42:18Z")

</div>

Hi @amidavidw,

Welcome aboard!

You can check the solution proposed by @GeMi: [Token Refresh - Java - Example](https://community.gatling.io/t/token-refresh-java-example/7935) .  
Does that suit your use case?

Cheers!

---

<div class="post-metadata">

**Author:** ![amidavidw](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@amidavidw](https://community.gatling.io/u/amidavidw)\
**Post date:** [March 18, 2024, 10:10pm UTC](https://community.gatling.io/t/get-auth-token-with-one-user-from-one-url-and-use-it-with-multiple-users-to-test-another-url/8816/4 "2024-03-18T22:10:57Z")

</div>

Hi @trinp, @sbrevet,

Thanks a lot for your help.  
I got it working 😄

After checking both of the solutions recommended,  
I noticed that the code snippet in my post is very similar to GeMi’s solution.

The fix is to replace

```java
        ScenarioBuilder scn = scenario("Book Store Test")
                .exec(s -> {
                    System.out.println("JWT to use: " + jwt.get(0));
                    s.set("jwtToken", jwt.get(0));
                    return s;
                })
                .exec(getAll)
                .exec(create);

```

with

```java
        ScenarioBuilder scn = scenario("Book Store Test")
                .exec(s -> s.set("jwtToken", jwt.get(0)))
                .exec(getAll)
                .exec(create);

```

What is the difference between these two `.exe(...)` ?

Should we always use `.exec(s -> s.set(...))` before and  
`.exec(s -> { ... return s;})` after the execution of the chanbuilder?

Thanks again!

---

<div class="post-metadata">

**Author:** ![amidavidw](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@amidavidw](https://community.gatling.io/u/amidavidw)\
**Post date:** [March 18, 2024, 10:12pm UTC](https://community.gatling.io/t/get-auth-token-with-one-user-from-one-url-and-use-it-with-multiple-users-to-test-another-url/8816/5 "2024-03-18T22:12:17Z")

</div>

Hi @GeMi

Sorry, only two mentions allowed in one post.  
Please check my reply above.

Thank you

---

<div class="post-metadata">

**Author:** ![GeMi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/gemi/32/939_2.png) [@GeMi](https://community.gatling.io/u/GeMi)\
**Post date:** [March 18, 2024, 10:23pm UTC](https://community.gatling.io/t/get-auth-token-with-one-user-from-one-url-and-use-it-with-multiple-users-to-test-another-url/8816/6 "2024-03-18T22:23:19Z")

</div>

From:

> **[Session API](https://docs.gatling.io/reference/script/core/session/api/#setting-attributes)**
>
> How to store and fetch data programmatically for your virtual users' Session, typically when using functions.

See below:

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/2/215b7c8e046f9d0864c75a68fe4361b561420cf0.png)

---

<div class="post-metadata">

**Author:** ![amidavidw](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@amidavidw](https://community.gatling.io/u/amidavidw)\
**Post date:** [March 18, 2024, 10:29pm UTC](https://community.gatling.io/t/get-auth-token-with-one-user-from-one-url-and-use-it-with-multiple-users-to-test-another-url/8816/7 "2024-03-18T22:29:04Z")

</div>

I see. Thank you very much

---

<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:** [April 17, 2024, 10:29pm UTC](https://community.gatling.io/t/get-auth-token-with-one-user-from-one-url-and-use-it-with-multiple-users-to-test-another-url/8816/8 "2024-04-17T22:29:26Z")

</div>

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