# Using feeder to build query string

**URL:** <https://community.gatling.io/t/using-feeder-to-build-query-string/626>\
**Category:** Gatling (Open-Source)\
**Created:** [October 21, 2013, 11:46am UTC](https://community.gatling.io/t/using-feeder-to-build-query-string/626 "2013-10-21T11:46:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ariel\_Kogan](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@Ariel\_Kogan](https://community.gatling.io/u/Ariel_Kogan)\
**Post date:** [October 21, 2013, 11:46am UTC](https://community.gatling.io/t/using-feeder-to-build-query-string/626/1 "2013-10-21T11:46:07Z")

</div>

Hi everyone,

I want to hit a url with a query string built based on a feeder. It’s easier with an example:

**ids.csv**  
id  
1  
2  
3  
4  
5

[http://localhost?ids=1,2,3](http://localhost?ids=1,2,3)

Each time I run the scenario, I’d like to have a different number of randomly picked IDs.  
[http://localhost?ids=2,5](http://localhost?ids=2,5)

[http://localhost?ids=1,3,5](http://localhost?ids=1,3,5)

etc.

I’ve tried to do the following but it doesn’t work.

val scn = scenario(“My scenario”)  
.feed(csv(“ids.csv”).random)  
.exec(  
http(“request\_1”)  
.get(session =\> “?ids=” + buildIdList)  
.check(status.is(200)))

// Will create a string like “{id}{id}{id}” with a random number of {id}  
def buildIdList = (0 to new Random().nextInt(10) + 1).map(n =\> “${id}”).toList.mkString(",")

I was hoping Gatling will replace each {id} in the built query string with a value from the feeder but id doesn’t.

I’d appreciate if you have any ideas. Thanks,

Ariel

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [October 21, 2013, 12:09pm UTC](https://community.gatling.io/t/using-feeder-to-build-query-string/626/2 "2013-10-21T12:09:52Z")

</div>

Hi,

Fist, you won’t be able to directly use the whole Feeder API, but you can re-use the Feeder utilities.

Then, I might be wrong, but it’s more likely that you actually need **unique** randomly picked ids.  
If so, a simple approach would be to inject into a Set, but if your possible values list is not big enough, collisions would be high, so your distribution would change and you’d need a more complex approach.

With Gatling 2:

val data: Array[Map[String, String]] = csv(“ids.csv”).data // get the underlying array

val feedIds = (session: Session) =\> {  
val random = ThreadLocalRandom.current  
val ids = (0 to random.nextInt(10 + 1).map(n = data(n)(“id”).toSet.mkString(",")  
session.set(“ids”, ids)  
}

exec(feedIds)  
.exec(http(“request\_1”).get("?ids=${ids}").check([status.is](http://status.is)(200)))

}

Get it?

Cheers,

Stéphane

---

<div class="post-metadata">

**Author:** ![Ariel\_Kogan](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@Ariel\_Kogan](https://community.gatling.io/u/Ariel_Kogan)\
**Post date:** [October 21, 2013, 2:00pm UTC](https://community.gatling.io/t/using-feeder-to-build-query-string/626/3 "2013-10-21T14:00:22Z")

</div>

Hi Stéphane,

Thanks for the quick response. I’ve been trying to make it work but I haven’t been successful.

exec seems to take a Session =\> Validation[Session] as a parameter, so I’ve changed the last line of feedIds to be

new Success(session.set(“ids”, ids))

I set the log level to DEBUG and when I try to run the simulation, apparently no requests are being made. I just read

14:53:46.947 [DEBUG] i.g.c.r.Runner - Finished Launching scenarios executions  
14:53:46.949 [INFO] i.g.c.a.UserStart - Start user #0  
14:53:46.949 [INFO] i.g.c.a.UserEnd - End user #0  
14:53:48.076 [INFO] i.g.c.a.UserStart - Start user #1  
14:53:48.076 [INFO] i.g.c.a.UserEnd - End user #1

Does it ring any bell? Thanks a lot,

Ariel

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [October 21, 2013, 2:05pm UTC](https://community.gatling.io/t/using-feeder-to-build-query-string/626/4 "2013-10-21T14:05:50Z")

</div>

> Hi Stéphane,
> 
> Thanks for the quick response. I've been trying to make it work but I  
> haven't been successful.
> 
> exec seems to take a Session =\> Validation[Session] as a parameter, so  
> I've changed the last line of feedIds to be
> 
> new Success(session.set("ids", ids))

There's an implicit conversion in core.Predef that turns T into  
Validation[T], so you should have been fine. Do you import core.Predef?

> I set the log level to DEBUG and when I try to run the simulation,  
> apparently no requests are being made. I just read
> 
> 14:53:46.947 [DEBUG] i.g.c.r.Runner - Finished Launching scenarios  
> executions  
> 14:53:46.949 [INFO] i.g.c.a.UserStart - Start user #0  
> 14:53:46.949 [INFO] i.g.c.a.UserEnd - End user #0  
> 14:53:48.076 [INFO] i.g.c.a.UserStart - Start user #1  
> 14:53:48.076 [INFO] i.g.c.a.UserEnd - End user #1
> 
> Does it ring any bell? Thanks a lot,

Usually happens when once forgets a dot, so that execs are not properly  
chained.

Could you share a gist, please?

---

<div class="post-metadata">

**Author:** ![Ariel\_Kogan](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@Ariel\_Kogan](https://community.gatling.io/u/Ariel_Kogan)\
**Post date:** [October 21, 2013, 2:14pm UTC](https://community.gatling.io/t/using-feeder-to-build-query-string/626/5 "2013-10-21T14:14:19Z")

</div>

[https://gist.github.com/arikogan/7084579](https://gist.github.com/arikogan/7084579)

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [October 21, 2013, 3:20pm UTC](https://community.gatling.io/t/using-feeder-to-build-query-string/626/6 "2013-10-21T15:20:21Z")

</div>

Regarding the implicit not happening, my fault (directly coded in gmail): you need to declare expected type to make implicit conversion happen:

import io.gatling.core.session.Expression

val feedIds: Expression[Session] = …

Otherwise, the “cool” way is:

import io.gatling.core.validation.\_

session.set(“ids”, ids).success

Regarding the “no requests”, as I expected, there’s a missing dot line 25 in your gist:

[https://gist.github.com/arikogan/7084579#file-retrieveids-scala-L25](https://gist.github.com/arikogan/7084579#file-retrieveids-scala-L25)

---

<div class="post-metadata">

**Author:** ![Ariel\_Kogan](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@Ariel\_Kogan](https://community.gatling.io/u/Ariel_Kogan)\
**Post date:** [October 21, 2013, 4:49pm UTC](https://community.gatling.io/t/using-feeder-to-build-query-string/626/7 "2013-10-21T16:49:21Z")

</div>

That’s great, that did work Stéphane. Thank you.

I’m still confused about when to use the methods imported by import bootstrap.\_ and when not to. That was the problem in this [thread](https://groups.google.com/forum/#!topic/gatling/A0DJbgYXVIA). If you can shed some light on that, that would be helpful.

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [October 21, 2013, 5:06pm UTC](https://community.gatling.io/t/using-feeder-to-build-query-string/626/8 "2013-10-21T17:06:58Z")

</div>

bootstrap is actually an object. importing bootstrap.\_ puts all its methods in scope, like exec.

It will be removed in M4.
