# Disappearing Exec Call

**URL:** <https://community.gatling.io/t/disappearing-exec-call/8226>\
**Category:** Gatling (Open-Source)\
**Created:** [August 21, 2023, 3:46pm UTC](https://community.gatling.io/t/disappearing-exec-call/8226 "2023-08-21T15:46:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![WeinYopp](https://avatars.discourse-cdn.com/v4/letter/w/4da419/32.png) [@WeinYopp](https://community.gatling.io/u/WeinYopp)\
**Post date:** [August 21, 2023, 3:46pm UTC](https://community.gatling.io/t/disappearing-exec-call/8226/1 "2023-08-21T15:46:17Z")

</div>

I am trying to run a session where one call depends on the result of another call. I have managed my way through many bugs to do with saving to Session, including a lot of compilation errors on trying to access session when the data didn’t exist yet, and then once data was saved successfully I had to remove special characters that were added (see `def req5` in code below).

Anyway, now I have it compiling, but I don’t see my actual request getting built. I don’t see anything to do with it in the logs actually - no reference to `Req5` as it appears in the request name, nor any part of the path that I’m querying (both these things appear in logs for all other requests).

Suppose the session variable I’m retrieving for req5 is saved via `check(BodyString.saveAs())` in req2, my code looks something like this:

```auto
  def req5(sessionStr: String): HttpRequestBuilder = http(s"MyScenario - Req5")
    .get(s"/path/to/query/${fromJsonString[MyProto](sessionStr.replace("\"", "").replace("\\", "\"")).field}")

  setUp(
    scenario("MyScenario")
      .exec(
        exec(req1),
        exec(req2),
        exec(req3),
        exec(req4),
      )
      .exec { session =>
        req5(session("myString").as[String])
        session
      }
      .inject(injectionProfile)

```

I have also tried the following instead of chaining req5 after all the others:  
`exec(req2).exec { session => req5(session("myString").as[String]) session } `  
with exact same results - ‘successful’ test passing with all requests showing success, but no trace of `req5`

Note: if I change it it to the following, I get an error saying `Session` does not contain `myString`

```auto
 setUp(
    scenario("MyScenario")
      .exec(
        exec(req1)
        exec(req2)
        exec(req3)
        exec(req4)
        exec(req5(session("myString").as[String]))
        session
       }
      .inject(injectionProfile)

```

Any help is greatly appreciated.

---

<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:** [August 21, 2023, 6:50pm UTC](https://community.gatling.io/t/disappearing-exec-call/8226/2 "2023-08-21T18:50:45Z")

</div>

Hi @WeinYopp,

Welcome aboard!

As stated [in the first note box of the documentation](https://gatling.io/docs/gatling/reference/current/core/concepts/):

> 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, you can’t call this way.

For your use case, I assume you’ll need to set a session variable with your wanted value (or use [transform](https://gatling.io/docs/gatling/reference/current/core/check/#transform) when getting your `"myString"`) and use that in a [Gatling Expression Language](https://gatling.io/docs/gatling/reference/current/core/session/el/)

Something like:

```scala
  val req5: HttpRequestBuilder = http(s"MyScenario - Req5")
    .get("/path/to/query/#{myPath}")

  setUp(
    scenario("MyScenario")
      .exec(
        req1,
        req2,
        req3,
        req4,
      )
      .exec { session =>
        session.set("myPath", fromJsonString[MyProto](session("myString").as[String].replace("\"", "").replace("\\", "\"")).field)
      }
      .exec(req5)
      .inject(injectionProfile)

```

I hope this helps!

Cheers!

---

<div class="post-metadata">

**Author:** ![WeinYopp](https://avatars.discourse-cdn.com/v4/letter/w/4da419/32.png) [@WeinYopp](https://community.gatling.io/u/WeinYopp)\
**Post date:** [August 22, 2023, 4:13pm UTC](https://community.gatling.io/t/disappearing-exec-call/8226/3 "2023-08-22T16:13:57Z")

</div>

Thanks for the quick response. I was able to use this to successfully make 1 request based on 1 reply. However I was hoping for a bit more…

First of all, I noticed some limitations of the DSL that are not mentioned anywhere in docs:  
a) I can’t use `exec{ session => ... }` with a request - it simply won’t run if you want to access session. This seems like it would be very good to have mentioned in docs because it caused me a lot of confusion.  
b) I can’t do multiple things in a session without assigning the session to a var… I expected that I could do multiple `set`s in the same `exec`, and then return session, but it seems to only use the last one I put… or example I did

```auto
session.set("req2Field", req2.field)
session.set("req2Size", req2.size)

```

but only size was there in the next `exec` call. I figured out that I can do this instead:

```auto
var s = session
s = s.set("req2Field", req2.field)
s = s.set("req2Size", req2.size)
s

```

but that is a bit cumbersome to write every time - especially when the whole point is to get around writing `exec{ session => ... }` for each item I want to save.

and c) I cannot seem to write `exec(req1, req2, ...)` as you have - instead I’m finding I have to write `exec(exec(req1), exec(req2), ...)`.

Secondly, I am still unsure if Gatling can fit my needs because I am hoping to be able to do one-response-to-many-requests flows with it. However this would rely on using a different identifier for each path interpolation, meaning I am currently trying to do the following:

```auto
    def req5s = (0 until req2Size).map(num =>
      http(s"StartScenario - GetAllEventsSchedForOrg$num")
        .get(
          s"/event/allData/status/${EventState.EVENT_STATE_SCHEDULED}/forOrg/#{orgId$num}"
        )
    )

setup(scnenario("name").exec(exec(req1), exec(req2),...).exec{session =>
  val req2Field = fromJsonString[Req2Proto](session("req2").as[String]).field
  req2Size = req2Field.size
  req2Field.zipWithIndex.foreach { case (item, index) =>
    s = s.set(s"req2field_$index", item)
  }
}.exec(req5s.map(exec)).inject(injectionProfile))

```

within an exec call. Note that `req2Size` is not retrieved from session but rather saved as a global `var` so I can write `exec(...)` instead of `exec{session => ...}`. The result of trying this is that the list `req5s` is totally ignored, and I am not sure why. I have also tried with `req5s.map(exec): _*`

---

<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:** [August 23, 2023, 7:36am UTC](https://community.gatling.io/t/disappearing-exec-call/8226/4 "2023-08-23T07:36:58Z")

</div>

Hi @WeinYopp,

> [@WeinYopp](#):
>
> a) I can’t use `exec{ session => ... }` with a request - it simply won’t run if you want to access session. This seems like it would be very good to have mentioned in docs because it caused me a lot of confusion.

As stated in my previous message, it is mentioned in the docs.  
`session => ...` is the syntax to create a function. You can’t define in your own function.

> [@WeinYopp](#):
>
> b) I can’t do multiple things in a session without assigning the session to a var… I expected that I could do multiple `set`s in the same `exec`, and then return session, but it seems to only use the last one I put… or example I did

Once again, it is written in the documentation: see the note box in [Session API - Setting attributes](https://gatling.io/docs/gatling/reference/current/core/session/session_api/#setting-attributes)

Do you know you can chain?

```scala
 .exec { session =>
    session.set("req2Field", req2.field)
      .set("req2Size", req2.size)
 }

```

> [@WeinYopp](#):
>
> Secondly, I am still unsure if Gatling can fit my needs

Please open another topic to explain your use case if you do think so.  
But, before, have you taken a look into the [Gatling Academy](https://academy.gatling.io/)?

And about using different path… you should use other [structure elements](https://gatling.io/docs/gatling/reference/current/core/scenario/#structure-elements)

Cheers!

---

<div class="post-metadata">

**Author:** ![WeinYopp](https://avatars.discourse-cdn.com/v4/letter/w/4da419/32.png) [@WeinYopp](https://community.gatling.io/u/WeinYopp)\
**Post date:** [August 23, 2023, 1:32pm UTC](https://community.gatling.io/t/disappearing-exec-call/8226/5 "2023-08-23T13:32:54Z")

</div>

Thanks for all this! I did not know about chaining session commands - that is helpful. I will check out Gatling Academy too

---

<div class="post-metadata">

**Author:** ![WeinYopp](https://avatars.discourse-cdn.com/v4/letter/w/4da419/32.png) [@WeinYopp](https://community.gatling.io/u/WeinYopp)\
**Post date:** [August 23, 2023, 1:57pm UTC](https://community.gatling.io/t/disappearing-exec-call/8226/6 "2023-08-23T13:57:58Z")

</div>

I did some googling on my issue and ended up [in this StackOverflow](https://stackoverflow.com/questions/72701911/gatling-execute-call-in-for-each-loop-using-values-from-list-stored-in-session) where I found a solution to my 1-reply-to-many-reqs requirement - `foreach` which can be used after `exec`. Unfortunately Academy did not help much with this, but at least I have found how to do it.

---

<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:** [August 23, 2023, 3:12pm UTC](https://community.gatling.io/t/disappearing-exec-call/8226/7 "2023-08-23T15:12:49Z")

</div>


