# How would you send the same request to multiple endpoints based on a list?

**URL:** <https://community.gatling.io/t/how-would-you-send-the-same-request-to-multiple-endpoints-based-on-a-list/1245>\
**Category:** Gatling (Open-Source)\
**Created:** [August 6, 2014, 1:40pm UTC](https://community.gatling.io/t/how-would-you-send-the-same-request-to-multiple-endpoints-based-on-a-list/1245 "2014-08-06T13:40:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jarrowwx](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@jarrowwx](https://community.gatling.io/u/jarrowwx)\
**Post date:** [August 6, 2014, 1:40pm UTC](https://community.gatling.io/t/how-would-you-send-the-same-request-to-multiple-endpoints-based-on-a-list/1245/1 "2014-08-06T13:40:45Z")

</div>

I have a scenario that loads a page and reads four RESTful services that return JSON objects.

I need to build a single JSON object to send back to the server, but I need to conditionally send it back based on what was in those JSON objects. Specifically, if the first service had zero objects returned, I skip it. If it had one, I send a request to the server with my built object. If it has two or more, I send a response to the server for each one. I need to repeat that with different URLs for each of those objects.

If I were doing this declaratively, or in native scala, I would just loop over the objects returned by the restful service and compose the appropriate URL and then send it.

What is the “Gatling-Way” of doing that?

---

<div class="post-metadata">

**Author:** ![jarrowwx](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@jarrowwx](https://community.gatling.io/u/jarrowwx)\
**Post date:** [August 6, 2014, 5:49pm UTC](https://community.gatling.io/t/how-would-you-send-the-same-request-to-multiple-endpoints-based-on-a-list/1245/2 "2014-08-06T17:49:37Z")

</div>

It appears to be .foreach() which I’m playing with. However, all is not well, yet…

I’m doing something like this:

.exec( session =\> session.set( THE\_ID\_LIST, “” ) )  
…  
.check( jsonPath("$.result[\*].id" ).findAll.dontValidate.saveAs( THE\_ID\_LIST )  
…  
.exec( session =\> {  
println( "THE\_ID\_LIST: " + session( THE\_ID\_LIST ).as[String] ) // THE\_ID\_LIST: Vector()  
session  
})  
…  
.doIf( session =\> session( THE\_ID\_LIST ).as[String] != “” ) {  
foreach( THE\_ID\_LIST, THE\_ID ) {  
…  
}  
}

Now, looking at the documentation, foreach takes a string, uses that as the key to find the Iterable in the Session, and iterates over it. THE\_ID\_LIST is a string constant. Clearly, the session contains a key for that string. But when I run it, I get an error:

13:44:19.139 [ERROR] i.g.c.s.LoopBlock$ - Condition evaluation crashed with message ‘Can’t cast value theIdList of type class java.lang.String into interface scala.collection.Seq’, exiting loop

Looks like we have a bug in there somewhere. FYI, I just tested with 2.0.0-RC1, same problem.

Thanks!

---

<div class="post-metadata">

**Author:** ![jarrowwx](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@jarrowwx](https://community.gatling.io/u/jarrowwx)\
**Post date:** [August 6, 2014, 5:53pm UTC](https://community.gatling.io/t/how-would-you-send-the-same-request-to-multiple-endpoints-based-on-a-list/1245/3 "2014-08-06T17:53:59Z")

</div>

On a whim, I tried modifying it to:

.foreach( “${” + THE\_ID\_LIST + “}”, THE\_ID ) {

and it looks like it may have actually worked. Is that the way it is supposed to be?

---

<div class="post-metadata">

**Author:** ![Nicolas\_R](https://avatars.discourse-cdn.com/v4/letter/n/f14d63/32.png) [@Nicolas\_R](https://community.gatling.io/u/Nicolas_R)\
**Post date:** [August 6, 2014, 5:56pm UTC](https://community.gatling.io/t/how-would-you-send-the-same-request-to-multiple-endpoints-based-on-a-list/1245/4 "2014-08-06T17:56:33Z")

</div>

Hi,

.check( jsonPath("$.result[\*].id" ). **findAll**.dontValidate.saveAs( THE\_ID\_LIST )

Can you try with .find instead ?

thanks  
Nicolas

---

<div class="post-metadata">

**Author:** ![jarrowwx](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@jarrowwx](https://community.gatling.io/u/jarrowwx)\
**Post date:** [August 6, 2014, 6:15pm UTC](https://community.gatling.io/t/how-would-you-send-the-same-request-to-multiple-endpoints-based-on-a-list/1245/5 "2014-08-06T18:15:13Z")

</div>

Does .find with a wildcard operate the same as .findAll()? Because the .findAll part is working fine.

The part that is not visible in my code snippet is this:

val THE\_ID\_LIST = “theIdList”

If I take out that indirection, it might become more clear what is going on:

foreach( “theIdList”, “theId” ) {

Does NOT work, but…

foreach( “${theIdList}”, “theId” ) {

Does. Is that expected behavior?

---

<div class="post-metadata">

**Author:** ![Pierre\_DAL-PRA](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@Pierre\_DAL-PRA](https://community.gatling.io/u/Pierre_DAL-PRA)\
**Post date:** [August 6, 2014, 6:27pm UTC](https://community.gatling.io/t/how-would-you-send-the-same-request-to-multiple-endpoints-based-on-a-list/1245/6 "2014-08-06T18:27:00Z")

</div>

Hi John,

This in an expected behaviour : _foreach_’s first parameter takes is an Expression[Seq[Any]], meaning an EL expression which resolves to a list or a function which takes a Session and returns a list.

This explains why “theIdList”, which is not an EL string, does not work whereas “${theIdList}” does.

---

<div class="post-metadata">

**Author:** ![Pierre\_DAL-PRA](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@Pierre\_DAL-PRA](https://community.gatling.io/u/Pierre_DAL-PRA)\
**Post date:** [August 6, 2014, 6:28pm UTC](https://community.gatling.io/t/how-would-you-send-the-same-request-to-multiple-endpoints-based-on-a-list/1245/7 "2014-08-06T18:28:33Z")

</div>

However, the documentation is not exactly clear on that matter, I’ll make sure to clear that out 😉

Cheers,

Pierre

---

<div class="post-metadata">

**Author:** ![jarrowwx](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@jarrowwx](https://community.gatling.io/u/jarrowwx)\
**Post date:** [August 6, 2014, 6:44pm UTC](https://community.gatling.io/t/how-would-you-send-the-same-request-to-multiple-endpoints-based-on-a-list/1245/8 "2014-08-06T18:44:27Z")

</div>

Thanks, guys!
