# Foreach issues ...

**URL:** <https://community.gatling.io/t/foreach-issues/3148>\
**Category:** Gatling (Open-Source)\
**Created:** [July 19, 2016, 6:14pm UTC](https://community.gatling.io/t/foreach-issues/3148 "2016-07-19T18:14:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aditya\_siram](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@aditya\_siram](https://community.gatling.io/u/aditya_siram)\
**Post date:** [July 19, 2016, 6:14pm UTC](https://community.gatling.io/t/foreach-issues/3148/1 "2016-07-19T18:14:51Z")

</div>

Hi all,  
I’m trying to use `foreach` to build up a scenario where I POST each item in a sequence but running the following error:  
[error] found : io.gatling.http.request.builder.HttpRequestBuilder  
[error] required: io.gatling.commons.validation.Validation[io.gatling.core.session.Session]

Here’s the scenario:  
val xs = 1 to 10  
val scn = scenario(“Test”)  
.foreach(xs, “x”) {  
exec(session =\> {  
val x = session(“x”).as[Integer]  
http(“sendTest”)  
.post("/blah/")  
.body(StringBody(s"${x}"))  
.check(status.is(200))  
})  
}

I feel like I’m doing something obviously wrong but I can’t seem to understand how to fix this from the documentation.

Thanks!  
-deech

---

<div class="post-metadata">

**Author:** ![aditya\_siram](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@aditya\_siram](https://community.gatling.io/u/aditya_siram)\
**Post date:** [July 19, 2016, 6:38pm UTC](https://community.gatling.io/t/foreach-issues/3148/2 "2016-07-19T18:38:24Z")

</div>

I got past that error by doing:  
val xs = (1 to 10)  
val scn = scenario(“Test”)  
.exec(session =\> {  
foreach(xs, “x”) {  
val x = session(“x”).as[Integer]  
System.out.println(x)  
exec(http(“post”)  
.post("/blah/")  
.body(StringBody(x.toString))  
.check(status.is(200)))  
}  
Success(session)  
})

but now I get:  
Simulation Test started…

---

<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 19, 2016, 7:29pm UTC](https://community.gatling.io/t/foreach-issues/3148/3 "2016-07-19T19:29:01Z")

</div>

From [doc](http://gatling.io/docs/2.2.2/general/scenario.html):

Gatling DSL components are immutable `ActionBuilder`(s) that have to be chained altogether and are only built once on startup. The results is a workflow chain of `Action`(s). These builders don’t do anything by themselves, they don’t trigger any side effect, they are just definitions. As a result, creating such DSL components at runtime in functions is completely meaningless. If you want conditional paths in your execution flow, use the proper DSL components (`doIf`, `randomSwitch`, etc)

```auto
exec { session =>

  if (someSessionBasedCondition(session)) {
    // just create a builder that is immediately discarded, hence doesn't do anything
    // you should be using a doIf here
    http("Get Homepage").get("[http://github.com/gatling/gatling](http://github.com/gatling/gatling)")
  }
  session
}

```

---

<div class="post-metadata">

**Author:** ![Mark\_Bordelon](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Mark\_Bordelon](https://community.gatling.io/u/Mark_Bordelon)\
**Post date:** [December 2, 2016, 12:11am UTC](https://community.gatling.io/t/foreach-issues/3148/4 "2016-12-02T00:11:48Z")

</div>

So, Stephane. Is there really no way to create actions in an ActionBuilder at runtime? How does the recorder do it? This would dash my hopes of creating a scenario from an textfile. C’est rellement pas possible?

---

<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:** [December 2, 2016, 6:56am UTC](https://community.gatling.io/t/foreach-issues/3148/5 "2016-12-02T06:56:00Z")

</div>

I suspect you’re confusing load time, when the Simulation instance is loaded and the scenario workflows are created, and runtime, when the virtual users are propelled along those workflows and for example execute functions.  
You can perfectly create scenarios based on some file content at load time.

---

<div class="post-metadata">

**Author:** ![Mark\_Bordelon](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Mark\_Bordelon](https://community.gatling.io/u/Mark_Bordelon)\
**Post date:** [December 5, 2016, 9:23pm UTC](https://community.gatling.io/t/foreach-issues/3148/6 "2016-12-05T21:23:24Z")

</div>

Stephane, vous avez raison. That was my confusion. Can you point me to some code that builds up scenarios dynamically at load time? I still have one day to put this together before a demo (am currently creating source code with shell scripts – quelle horreur)

---

<div class="post-metadata">

**Author:** ![Nikolay\_Degkin](https://avatars.discourse-cdn.com/v4/letter/n/a6a055/32.png) [@Nikolay\_Degkin](https://community.gatling.io/u/Nikolay_Degkin)\
**Post date:** [December 13, 2016, 2:41pm UTC](https://community.gatling.io/t/foreach-issues/3148/7 "2016-12-13T14:41:55Z")

</div>

If i understood it right, you want to have 10 requests with appropriate body from 1 to 10?  
It yes, you can try smth like below. I use Get just to see my requests

`  
val endNum = 5  
val startNum = 1  
var counter = new java.util.concurrent.atomic.AtomicInteger(startNum-1)

val httpProtocol = http  
.baseURL(“[http://demo.mockable.io/](http://demo.mockable.io/)”)

val scn = scenario("+1")  
.asLongAs(session =\> counter.getAndIncrement() \< endNum)  
{  
exec(http(“Change\_Password”)  
.get(session =\>“[http://demo.mockable.io/](http://demo.mockable.io/)” + s"""${counter}""")  
.check(status.is(404))  
)  
}  
`
