# Modifying session in transformWithSession in Java

**URL:** <https://community.gatling.io/t/modifying-session-in-transformwithsession-in-java/9722>\
**Category:** Gatling (Open-Source)\
**Created:** [July 16, 2025, 9:08am UTC](https://community.gatling.io/t/modifying-session-in-transformwithsession-in-java/9722 "2025-07-16T09:08:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tgrzej-avaloq](https://avatars.discourse-cdn.com/v4/letter/t/ba8739/32.png) [@tgrzej-avaloq](https://community.gatling.io/u/tgrzej-avaloq)\
**Post date:** [July 16, 2025, 9:08am UTC](https://community.gatling.io/t/modifying-session-in-transformwithsession-in-java/9722/1 "2025-07-16T09:08:13Z")

</div>

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

I made sure I’ve update my Gatling version to the latest release  
 I read the [guidelines](https://community.gatling.io/t/welcome-in-the-gatling-community-our-code-of-conduct/6462) and [how to ask a question](https://community.gatling.io/t/how-to-ask-a-question/6538) topics.  
 I provided a [SSCCE](http://www.sscce.org/) (or at least, all information to help the community understand my topic)  
 I copied output I observe, and explain what I think should be.

Hello Gatling Community,  
I would like to ask if it is legal and supposed to “work-as-desired” to modify the session within the `transformWithSession` method?

```auto
check((header("foo").transformWithSession((h, s) -> s.set("header_foo", h)))

```

The above effectively sets the key “header\_foo” in the session, but is it a proper use of the API or more of a coincidence?

Regards,  
Tomasz G.

---

<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:** [July 16, 2025, 1:45pm UTC](https://community.gatling.io/t/modifying-session-in-transformwithsession-in-java/9722/2 "2025-07-16T13:45:34Z")

</div>

No, what you’re doing doesn’t work. Session is immutable.  
What you’re simply doing here is creating a new Session instance that gets discarded right away as your check doesn’t do anything with it.  
You can’t transform the Session with `transformWithSession` whose purpose is to transform what’s extracted from the check.

---

<div class="post-metadata">

**Author:** ![tgrzej-avaloq](https://avatars.discourse-cdn.com/v4/letter/t/ba8739/32.png) [@tgrzej-avaloq](https://community.gatling.io/u/tgrzej-avaloq)\
**Post date:** [July 17, 2025, 9:58am UTC](https://community.gatling.io/t/modifying-session-in-transformwithsession-in-java/9722/3 "2025-07-17T09:58:24Z")

</div>

I am sorry, I simplified the example to make it more compact and I did not test it and… it was deprived of its vital content that way.

```auto
check(header("foo").transformWithSession((h, s) -> {
  Map<String, String> headers = s.getMap("headers");
  headers.put("foo", h);
  return s.set("headers", headers);
}))

```

This now becomes clear why it “works” :). The map inside the session gets updated with the additional value. The updated and returned session has nothing to do with that update and just gets discarded (one could return an empty string with the same effect).

The above is therefore not an example for a proper API usage and more of a result of some confusion on our end.

Greetings,  
Tomasz G.

---

<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 16, 2025, 9:58am UTC](https://community.gatling.io/t/modifying-session-in-transformwithsession-in-java/9722/4 "2025-08-16T09:58:43Z")

</div>

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