# UrlEncode queryParams and Ids per User

**URL:** <https://community.gatling.io/t/urlencode-queryparams-and-ids-per-user/721>\
**Category:** Gatling (Open-Source)\
**Created:** [December 11, 2013, 8:49pm UTC](https://community.gatling.io/t/urlencode-queryparams-and-ids-per-user/721 "2013-12-11T20:49:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Annie](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Annie](https://community.gatling.io/u/Annie)\
**Post date:** [December 11, 2013, 8:49pm UTC](https://community.gatling.io/t/urlencode-queryparams-and-ids-per-user/721/1 "2013-12-11T20:49:59Z")

</div>

I’ll start with I’m new to both Gatling and Scala, and I’m using Gatling-1.5.3

I’m having two issues with a scenario I’d like to create. The basic setup is this:

val divisionScn = scenario(“Division CRUD”)  
.exec(  
http(“Create Division”)  
.get("/division/add")  
.queryParam(“parameters”, java.net.URLEncoder.encode("""[{“divisionName”:“Perf Test Division”,“divisionCode”:“DivCode-${id}”]""", “UTF-8”))  
.check(status.is(200))  
)  
.pause(0 milliseconds, 5 milliseconds)  
.exec (  
http(“Update Division”)  
.get("/division/update")  
.queryParam(“parameters”, java.net.URLEncoder.encode("""[{“divisionName”:“Update name”,“divisionCode”:“DivCode-${id}”]""", “UTF-8”))  
.check(status.is(200))  
)  
.pause(0 milliseconds, 5 milliseconds)  
.exec (  
http(“Delete Division”)  
.get("/division/delete")  
.queryParam(“parameters”, java.net.URLEncoder.encode("""[{“divisionCode”:“DivCode-${id}”]""", “UTF-8”))  
.check(status.is(200))  
)

For each user, I’d like to increment an ID that becomes part of the division identifier “divisionCode”. I’ve found some examples of using AtomicInteger for that, which I am trying to use on the session, but I can’t figure out if it is working because it seems that encode method is executed at compile time and not at runtime, so I end up with requests that look like this:

GET [http://localhost:8080/division/add?parameters=%5B%7B%22divisionName%22%3A%22Perf%2BTest%2BDivision%22%2C%22divisionCode%22%3A%2](http://localhost:8080/division/add?parameters=%255B%257B%2522divisionName%2522%253A%2522Perf%2BTest%2BDivision%2522%252C%2522divisionCode%2522%253A%252)  
522 **DivCode-%2524%257Bid%257D** %2522%255D

instead of the id being replaced.

I’ve tried so many things and I can’t tell if I’m getting hung up on Scala or Gatling. Any advice greatly appreciated.

Annie

---

<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:** [December 11, 2013, 8:58pm UTC](https://community.gatling.io/t/urlencode-queryparams-and-ids-per-user/721/2 "2013-12-11T20:58:14Z")

</div>

“it seems that encode method is executed at compile time and not at runtime”  
Absolutely.

Simply, do you call it? Gatling is managing that.

---

<div class="post-metadata">

**Author:** ![Annie](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Annie](https://community.gatling.io/u/Annie)\
**Post date:** [December 11, 2013, 9:08pm UTC](https://community.gatling.io/t/urlencode-queryparams-and-ids-per-user/721/3 "2013-12-11T21:08:36Z")

</div>

Right after I posted, I found something that helped me out. I changed

.queryParam(“parameters”, java.net.URLEncoder.encode("""[{“divisionName”:“Update name”,“divisionCode”:“DivCode-${id}”]""", “UTF-8”))

to

.queryParam(“parameters”, (s:Session) =\> java.net.URLEncoder.encode("""[{“divisionName”:“Perf Test Division”,“divisionCode”:“DivCode-”""+s.userId.toString+""""]""",“UTF-8”))

and that seems to work.

I’m not sure how to answer the question “do you call it”. I was getting UrlEncode errors when trying this:  
.queryParam(“parameters”, “”"[{“divisionName”:“PerfTest Division”,“divisionCode”:“PerfDivision”}]""")  
which is why I added the call to java.net.URLEncoder.encode

---

<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:** [December 11, 2013, 9:15pm UTC](https://community.gatling.io/t/urlencode-queryparams-and-ids-per-user/721/4 "2013-12-11T21:15:54Z")

</div>

Yes, that’s the way.  
With your first tentative, the sequence was:

1. simulation loading: URLEncoder.encode

2. simulation loading: EL compiler triggered, but characters such as curly braces have been encoded, so it’s just compiled into a function that returns a constant string

3. every time a user reaches this action, the function is resolved, but the result is just a constant  
If you want dynamic stuff, you have to either pass a function, or an EL string. AND you cannot use EL inside a function.

---

<div class="post-metadata">

**Author:** ![Annie](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Annie](https://community.gatling.io/u/Annie)\
**Post date:** [December 11, 2013, 9:20pm UTC](https://community.gatling.io/t/urlencode-queryparams-and-ids-per-user/721/5 "2013-12-11T21:20:19Z")

</div>

Thanks for the explanation and the quick response!

---

<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:** [December 11, 2013, 9:54pm UTC](https://community.gatling.io/t/urlencode-queryparams-and-ids-per-user/721/6 "2013-12-11T21:54:43Z")

</div>

What is the error you got ?  
And also, just to be sure, in your Gatling config file, “useRawUrl” is correctly set to false ?

thanks.

---

<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:** [December 12, 2013, 6:14am UTC](https://community.gatling.io/t/urlencode-queryparams-and-ids-per-user/721/7 "2013-12-12T06:14:46Z")

</div>

Just providing some explanations about Nicolas’ questions: async-http-client (the library Gatling uses underneath) is supposed to take care on encoding query parameters, expect if you disabled the feature and set useRawUrl to true. So, you shouldn’t have to encode yourself.

I tested with 1.5.3 and master:

exec(http(“Search”)  
.get("/computers")  
.queryParam(""“f”"", “”"[{“divisionName”:“PerfTest Division”,“divisionCode”:“PerfDivision”}]"""))

properly sends:  
GET /computers?f=%5B%7B%22divisionName%22%3A%22PerfTest%20Division%22%2C%22divisionCode%22%3A%22PerfDivision%22%7D%5D HTTP/1.1

---

<div class="post-metadata">

**Author:** ![Annie](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Annie](https://community.gatling.io/u/Annie)\
**Post date:** [December 12, 2013, 3:20pm UTC](https://community.gatling.io/t/urlencode-queryparams-and-ids-per-user/721/8 "2013-12-12T15:20:52Z")

</div>

I went back to see what error I was getting, because I don’t want to have to do the extra encoding. Looks like I caused my own error and unfortunately spent the day trying to “fix” it. Amazing what becomes obvious after a good nights sleep.

Before I changed the logging levels to see the request I tried something with requestInfoExtractor, and I guess I got it wrong, saw the encoding exception and jumped to conclusions. At least I learned some things along the way.

Thanks for your help.

---

<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:** [December 12, 2013, 3:22pm UTC](https://community.gatling.io/t/urlencode-queryparams-and-ids-per-user/721/9 "2013-12-12T15:22:40Z")

</div>

That’s good news.

Thanks for the heads up.

cheers  
Nicolas
