Replacing substring in request body with a value from session

4,394 views
Skip to first unread message

Risto Yrjänä

unread,
May 8, 2014, 2:16:46 PM5/8/14
to gat...@googlegroups.com
Hi. 

I need to save a changing value from a HTTP response and then use that value as a part of some of the requests I make. What I have so far is:

check(regex("""someRegexp""").saveAs("key")))

which works fine. And then later when I do

exec(http("Foobar")
      .post( """someParams""")
      .headers(headers_1)
      .body(RawFileBody("RecordedComplexBody.txt"))

I would like to replace one part in the body txt with the value saved in "key". What would be the cleanest way of doing this? I can modify the body txt freely.

Stéphane Landelle

unread,
May 8, 2014, 2:33:53 PM5/8/14
to gat...@googlegroups.com
See https://github.com/excilys/gatling/wiki/Gatling%202#bodies

IMHO, that's more a matter of personal preferences: either you prefer having your templates as external files, so go with ELFileBody (use the Gatling EL syntax: ${key}) or use StringBody and host your templates into your Scala code (define a multiline Scala string inside triple quotes and use the same Gatling EL syntax):

val template = """{
  "foo": "${key}"
}"""

Get it?



--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Risto Yrjänä

unread,
May 9, 2014, 1:37:28 AM5/9/14
to gat...@googlegroups.com
Hi. 

That worked perfectly, thanks. ELFileBody was the class I was missing.

Stéphane Landelle

unread,
May 9, 2014, 2:44:04 AM5/9/14
to gat...@googlegroups.com
Glad it helped.
Have fun!

Gautam Soni

unread,
May 14, 2015, 5:08:09 PM5/14/15
to gat...@googlegroups.com
Can this help if I read the content from a csv feeder, and the value I needed to correct is one of the column values, which holds strings like "abc:xyz,pqr:${name}"

Sai Harish

unread,
Oct 25, 2018, 11:16:40 PM10/25/18
to Gatling User Group
Hi,

I am trying something similar as below

var ID_replace = System.nanoTime().toString()
println (ID_replace)

val postBody = """{
"Id": "${ID_replace}"
}"""

val scn = scenario("POSTMessage")
.exec(http("request_0")
.post("someURL")
.headers(headers_0)
.body(ElFileBody(postBody)))

But when I execute the script I always get "Attribute ID_replace is not defined".
Where did I go wrong ?

Abhishek S

unread,
Oct 26, 2018, 3:03:24 AM10/26/18
to gat...@googlegroups.com
You to set this value to session and access it.i can see you are printing it..
Session.set(string,string)

--

Sai Harish

unread,
Oct 26, 2018, 5:13:37 AM10/26/18
to Gatling User Group
Hi,

I have modified my code as below but still getting "Attribute ID_replace is not defined".

var ID_replace = System.nanoTime().toString()
println (ID_replace)

scenario("POSTMessage").exec { 
session => session.set("foo", ID_replace) 
}

val postBody = """{
"Id": "${ID_replace}"
}"""

val scn = scenario("POSTMessage")
.exec(http("request_0")
.post("someURL")
.headers(headers_0)
.body(ElFileBody(postBody)))

Sorry is this is a basic question !! I am new to Gatling and scala.

Alexandre CHAOUAT

unread,
Oct 26, 2018, 8:40:27 AM10/26/18
to gat...@googlegroups.com
Hi,

Indeed, to add an attribute to the session, you need to use the set method.

But the problem here, is that you are just declaring a first scenario in the middle of nowhere, and you don't keep a reference to it. Then you are declaring the real scenario that you are going to use. If you want something to happen in your scenario, that's in this scenario that you should declare things. Gatling will not magically link your two scenarios.

Furthermore, ElFileBody is a method to generate a body from a file. Its argument is a path, not a body. What you need to use is the StringBody method.

And why put this variable in the session ? 


Sai Harish

unread,
Oct 29, 2018, 1:06:53 AM10/29/18
to Gatling User Group
Thanks to all who helped. It worked.
Reply all
Reply to author
Forward
0 new messages