# Help passing a saved session variable to a Java method

**URL:** <https://community.gatling.io/t/help-passing-a-saved-session-variable-to-a-java-method/8433>\
**Category:** Gatling (Open-Source)\
**Created:** [November 9, 2023, 3:13pm UTC](https://community.gatling.io/t/help-passing-a-saved-session-variable-to-a-java-method/8433 "2023-11-09T15:13:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gkandale](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@gkandale](https://community.gatling.io/u/gkandale)\
**Post date:** [November 9, 2023, 3:13pm UTC](https://community.gatling.io/t/help-passing-a-saved-session-variable-to-a-java-method/8433/1 "2023-11-09T15:13:32Z")

</div>

Hi all, I’m having a hard time passing a variable from my GET call to my DELETE call for some reason. I’ve done this a million times but in this case it does not work for me. Any help would be appreciated!! 😃

```auto
    public static final ChainBuilder getListOfStuff =
            exec(http("Get List of Stuff")
                    .get("/example/api/v1/stuffs")
                    .headers(getSessionHeaders())
                    .check(status().is(200))
                    .check(jsonPath("$..id").exists().saveAs("stuffsId"))
            );

    public static final ChainBuilder deleteStuffIfCurrentUserIsOwnerOfStuff;
    static {
        try {
            deleteStuffIfCurrentUserIsOwnerOfStuff =
                    exec(http("Delete Stuff If Current User Is Owner")
                            .delete("/example/api/v1/stuffs")
                            .headers(getStuffSessionHeaders("#{stuffsId}"))
                            .check(status().is(204))
                    );
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static Map<String, String> getStuffSessionHeaders(String stuffsId) throws IOException {
        System.out.println("stuffsId=========" + stuffsId);
...................
}

```

The result I get from trying to pass **#{stuffsId}** to a java method is  
“stuffId=========#{stuffsId}” instead of the actual stuff ID i’m saving in GET from previous call.

Any help would be appreciated!

Thanks

---

<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:** [November 9, 2023, 3:17pm UTC](https://community.gatling.io/t/help-passing-a-saved-session-variable-to-a-java-method/8433/2 "2023-11-09T15:17:50Z")

</div>

Hi @gkandale,

Your issue lie in the fact that `headers` take a `Map<String, String>` as parameter and doesn’t involve Gatling Expression Language.

For the particular header you want with value from session (by expression language, for instance), you should use the `header` (singular) method.

Hope that helps,

Cheers!

---

<div class="post-metadata">

**Author:** ![gkandale](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@gkandale](https://community.gatling.io/u/gkandale)\
**Post date:** [November 9, 2023, 3:26pm UTC](https://community.gatling.io/t/help-passing-a-saved-session-variable-to-a-java-method/8433/3 "2023-11-09T15:26:46Z")

</div>

Hey thank you Sébastien for your super fast response! Thats awesome!!  
Can you give me an example? I don’t completely understand ☹

Thanks again!!  
George

---

<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:** [November 9, 2023, 3:33pm UTC](https://community.gatling.io/t/help-passing-a-saved-session-variable-to-a-java-method/8433/4 "2023-11-09T15:33:06Z")

</div>

This scenario will sent the value `#{hello}` without replacing it in the header.

```java
  exec(session -> session.set("hello", "world"))
  .exec(http("Get List of Stuff")
          .get("/api/endpoint")
          .headers(Map.of("X-Hello", "#{hello}"))
      );

```

But the following one will set the `X-Hello` header to the right value:

```java
  exec(session -> session.set("hello", "world"))
  .exec(http("Get List of Stuff")
          .get("/api/endpoint")
          .header("X-Hello", "#{hello}")
      );

```

Does that help?

Cheers!

---

<div class="post-metadata">

**Author:** ![gkandale](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@gkandale](https://community.gatling.io/u/gkandale)\
**Post date:** [November 9, 2023, 3:45pm UTC](https://community.gatling.io/t/help-passing-a-saved-session-variable-to-a-java-method/8433/5 "2023-11-09T15:45:51Z")

</div>

Hey Sebastian,

I’m trying to pass a variable to a method call which builds out the headers, including calculating the bearer token, but the stuff ID i’m passing to the method does not send the value, it sends “#{stuffId}” as the actual string

```auto
    public static final ChainBuilder deleteStuffIfCurrentUserIsOwnerOfStuff;
    static {
        try {
            deleteStuffIfCurrentUserIsOwnerOfStuff =
                    exec(http("Delete Stuff If Current User Is Owner")
                            .delete("/example/api/v1/stuffs")
                            .headers(getStuffSessionHeaders("#{stuffsId}"))
                            .check(status().is(204))
                    );
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static Map<String, String> getStuffSessionHeaders(String stuffsId) throws IOException {
        System.out.println("stuffsId=========" + stuffsId);
        
        STUFF_HELPER = new STUFFHelper(context);
        String stuffAccessToken = STUFF_HELPER.getSwitchStuffToken(stuffsId, getUserTokens());

        Map<String, String> stuffsSessionHeaders = new HashMap<String, String>();
        stuffsSessionHeaders.put("content-Type", "application/json");
        stuffsIdSessionHeaders.put("accept", "application/json");
        stuffsIdSessionHeaders.put("authorization", "Bearer " + accountAccessToken);
        return accountSessionHeaders;
    }

```

---

<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:** [November 9, 2023, 3:51pm UTC](https://community.gatling.io/t/help-passing-a-saved-session-variable-to-a-java-method/8433/6 "2023-11-09T15:51:25Z")

</div>

As stated in the first alert box of the [Concepts](https://gatling.io/docs/gatling/reference/current/core/concepts/#dsl) page:

> The components of this DSL are mere **definitions** of the desired effect. They don’t generate the desired effect where you invoke them in your own code. Only when chained with other components so they are ultimately passed to the [setUp](https://gatling.io/docs/gatling/reference/current/core/simulation/#setup), can the Gatling engine interpret them and produce the desired effect.

So, the call to your `getStuffSessionHeaders` is executed during the initialization phase. Way before the first virtual user is created.

Cheers!

---

<div class="post-metadata">

**Author:** ![gkandale](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@gkandale](https://community.gatling.io/u/gkandale)\
**Post date:** [November 9, 2023, 4:00pm UTC](https://community.gatling.io/t/help-passing-a-saved-session-variable-to-a-java-method/8433/7 "2023-11-09T16:00:37Z")

</div>

I’m trying to change tokens mid test - user needs a space specific token after they sign up to become an owner of some space. I need to get a new token for the user to be able to work within their specific space (including deleting their space after they are done with it. Any thought on how I can do this within gatling?

---

<div class="post-metadata">

**Author:** ![gkandale](https://avatars.discourse-cdn.com/v4/letter/g/c2a13f/32.png) [@gkandale](https://community.gatling.io/u/gkandale)\
**Post date:** [November 10, 2023, 2:40pm UTC](https://community.gatling.io/t/help-passing-a-saved-session-variable-to-a-java-method/8433/8 "2023-11-10T14:40:14Z")

</div>

Figured it out…wrote a gatling component instead of reusing the automation code that I’m trying to integrate with ☹

---

<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 10, 2023, 2:40pm UTC](https://community.gatling.io/t/help-passing-a-saved-session-variable-to-a-java-method/8433/9 "2023-12-10T14:40:27Z")

</div>

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