UrlEncode queryParams and Ids per User

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
522DivCode-%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

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

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

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

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.

Thanks for the explanation and the quick response!

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

thanks.

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

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.

That’s good news.

Thanks for the heads up.

cheers
Nicolas