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.
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?
Hi.
That worked perfectly, thanks. ELFileBody was the class I was missing.
Glad it helped.
Have fun!
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}”
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 ?
You to set this value to session and access it.i can see you are printing it…
Session.set(string,string)
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.
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 ?
Thanks to all who helped. It worked.