Gatling 2 - need example of handling dynamic values in json request files using both templates and objects

Hi,

I am using Gatling 2

I am working on the task of handling dynamic value in json request file body - as per HTTP template documentation for Gatling1.

json request is recorded in a text files e.g. “request_4.txt”

This is what I have done to change value dynamically using templates (.ssp):

  1. renamed request_4.txt to request_4.ssp (it is under /user-files/request-bodies)
  2. modified the content of request_4.ssp as following:

[
{
“id” : 13183,
“time” : “<%= timevalue %>”,
“login” : 2986,
“objects” : [
{
“id” : -23,
//…
“number” : 1

}
]
}
]

  1. changed code in recorded simulation:

val scn = scenario(“Create Leg”)

//…
.exec(http(“request_4”)

.post("""/api/sync/137/13183""")
.headers(headers_1)
.fileBody(“request_4”, Map(“timevalue” → “1374062413”))
// .body(RawFileBody(“request_4.txt”)) // this is what has been recorded

I get syntax error - it does not like fileBody - that is expected. Having read the Gatling 2 update page I figured out there were changes in the API, I fiddled a little bit but no luck.

It would be great if you could provide me and community with an example of solving this task in gatling 2:

  1. using templates
  2. using objects (as mentioned on the update page) - example at the bottom of this section does not provide enough information to get me started.

Many thanks in advance,
Natalie

  • Template:

Probably the easiest solution for non Scala devs.

request_4.txt:

[
{
“id” : 13183,
“time” : “${timevalue}”, // use regular Gatling EL
“login” : 2986,
“objects” : [
{
“id” : -23,
//…
“number” : 1

}
]
}
]

val scn = scenario(“Create Leg”)
//…
.exec(http(“request_4”)

.post("""/api/sync/137/13183""")
.headers(headers_1)
.body(ELFileBody(“request_4.txt”)) // assume that the Session contains a “timevalue” attribute (from feeder, check.saveAs, or else)

  • Programmatically

You have to be familiar with for comprehension and string interpolation in Scala.

val template: Expression[String] = (session: Session) =>
for {
timevalue = session(“timevalue”).validate[String]
} yield s"""[
{
“id” : 13183,
“time” : “$timevalue”, // string interpolation
“login” : 2986,
“objects” : [
{
“id” : -23,
//…
“number” : 1

}
]
}
]"""

val scn = scenario(“Create Leg”)
//…
.exec(http(“request_4”)

.post("""/api/sync/137/13183""")
.headers(headers_1)
.body(StringFileBody(template))

Get it?

Stéphane

Thanks Stéphane!

I see you have dropped SSP support so EL is the way to go for me - I want to make it work first than experiment with the string interpolation option later.

The simulation runs fine but cannot find timevalue

“01:19:55.121 [ERROR] i.g.h.a.HttpRequestAction - No attribute named ‘timevalue’ is defined”

I calculate timevalue programatically and need to set this as an attribute to a session I guess.

However this does not work:

val scn = scenario(“Create Leg”)
//…
.exec(session =>
session.set(“timevalue”, “1374062413”))

.exec(http(“request_4”)

.post("""/api/sync/137/13183""")
.headers(headers_1)
.body(ELFileBody(“request_4.txt”)) // assume that the Session contains a “timevalue” attribute (from feeder, check.saveAs, or else)

Thanks,

Looks good to me.
Could you paste your template file, please?

Hi Stéphane,

actually this code as specified above works now.
(I had to tidy up some other bits and pieces in simulation class).

Thanks so much for your help - it is invaluable.

I will try the other solution in a short while.

—Natalie

Great!