5

How to use session variable in StringBody of gatling?

I have defined my exec like,

val migrateAsset = exec(_.set("assetId", AssetIdGenerator.generateRandomAssetId()))
      .exec(http("Migrate Asset")
      .post(s"$url/asset/metadata")
      .header("Content-Type", "application/json")
      .header("Authorization", s"Bearer ${authToken}")
      .body(StringBody(
          s"""
            |{
            |    "objectType" : "DocumentType",
            |    "fileName" : "main.xml",
            |    "locations" : [
            |        {
            |            "region" : "eu-west-1",
            |            "url" : "https://s3-eu-west-1.amazonaws.com/${bucketName}/${assetId}"
            |        },
            |        {
            |            "region" : "us-east-1",
            |            "url" : s"https://s3.amazonaws.com/${bucketName}/${assetId}"
            |        }
            |    ],
            |    "format" : "MAIN",
            |    "mimeType" : "text/plain"
            |}
          """.stripMargin
      ))
      .check(status.is(200)))

In the body, I want same assetId to be passed for both eu-west and us-east regions. Since, assetId is randomly generated, I have stored it in session variable so as to make sure, I use same assetId for both locations.

But I cant pass assetId in StringBody format. It keeps giving me error like,

AssetsMigrationLoadSimulation.scala:31: not found: value assetId | "url" : "https://s3-eu-west-1.amazonaws.com/${bucketName}/${assetId}"

Aditya
  • 1,334
  • 1
  • 12
  • 23
  • Have you tried using an expression function like this: `.body(StringBody(session => """{ "myContent": """" + someGenerator(session) + """" }""")).asJSON` from the [docs](http://gatling.io/docs/current/http/http_request/#http-request-body) – Phonolog Aug 07 '17 at 06:48
  • 1
    Not quite sure, but you've got a lot of s"..." in there. This means Scala will evaluate the EL constructs itself, and not Gatling. If you remove the s prefix, it might work. – Dr. Hans-Peter Störr Jan 29 '19 at 14:33

1 Answers1

4

As Hans-Peter mentioned - your IDE has seen the ${...} that you're using to reference a gatling session parameter and decided that you are trying to do regular scala string interpolation - so it's put the 's' in front of the string.

remove the 's' and this should work

James Warr
  • 2,552
  • 2
  • 7
  • 20