Gatling - Using inputs from both csv feed and global variable

Hello,

I am using a CSV feed in my gatling script to retrieve username and few other fields. I also have another global variable called CLIENT_ID which is not part of the csv file. When I try to run the script, the CLIENT_ID is not passed in the body whereas the username field gets correctly sent.

Whats the best way to go about? Also do I need to use triple quotes “”" in the StringBody? Is there another/best way to achieve this?

val account = feed(userAccountList)
.exec(http(“Create Account”)
.post(URL)
.headers(headerMaps)
.body(StringBody("""{“ClientId”:"${CLIENT_ID}",“Username”:"${username}"}"""))
.check(bodyString.saveAs(“CreateUserResponse”))
)
.exec(session => {
println(session)
session
})

The “${CLIENT_ID}” expression looks for a value stored in the session. It does not allow you to access arbitrary Scala variables. In order to get what you want, you’ll want to make a minor tweak:

val account =
feed(userAccountList)
.exec( _.set(“CLIENT_ID”, CLIENT_ID) ) // need to add this line to store the variable into the session
.exec(
http(“Create Account”)
.post(URL)
.headers(headerMaps)
.body(StringBody("""{“ClientId”:"${CLIENT_ID}",“Username”:"${username}"}"""))
.check(bodyString.saveAs(“CreateUserResponse”))
)
.exec(session => {
println(session)
session
})

Thank you. Will try this out and let you know.

Can you please help on the second part of the question?
Also do I need to use triple quotes “”" in the StringBody? Is there another/best way to achieve this?

It’s standard Scala syntax. The use of triple quotes is so that you don’t have to escape the double quotes within the string.

You have plenty of options, including escaping the quotes, or using file bodies. Not sure any of them are better than the other, just different approaches, each with their own pros and cons.

If you are new to Scala, I would encourage you to learn more about the language in order to help you be as efficient and productive as possible. :slight_smile:

Thanks for your quick response and assistance. Both my queries are now answered.