How to pass a List (session (String attribute)) in a loop to Create a Bulk Json Request in Gatling Scala

How to pass a List (session (String attribute)) to a logic to Create a Bulk Json Request in Gatling Scala

From the response of this kind of request I am saving session Attributes which are List(String1…StringN)

    .exec(http("Find Perftest")
      .get("/Perftest")
      .queryParam("PerftestId", PerftestId)
      .check(jsonPath("$.results[*].name").findAll.saveAs("PerfName"))
      .check(jsonPath("$.results[*].id").findAll.saveAs("PerfID"))
      .check(jsonPath("$.results[*].type.id").findAll.saveAs("TypeID"))
      .check(status.is(200)))

I am trying to build a dynamic Bulk Json request from the saved session attributes (List)

    .exec { session =>
      val PerfNamex = session("PerfName").as[List[String]]
      val PerfIDx = session("PerfID").as[List[String]]
      val TypeIDx = session("PerfID").as[List[String]]
      val i =0
      val data1 = (i  to 2)
        .map { r =>
          Json.toJson(Map(
            "name" -> Json.toJson(s"${PerfName(i)}"),
            "assetId" -> Json.toJson(s"${PerfIDx(i)}"),
            "typeId" -> Json.toJson(s"${TypeIDx(i)}"))   }

      val data2 = Json.toJson(data1)
      session
    }

Passing the created Bulk Json to the below request

    .exec(http("exec Bulk Perftest")
      .post("/Perftest/bulk")
      .body(StringBody(session =>
        s"""${session("data2")}""".stripMargin)).asJson)

resulted in Error Bulk Json Request:

Body:StringRequestBody{charset=UTF-8, content=SessionAttribute(Session(Bulkperftestexe,1,HashMap(gatling.http.cache.baseUrl -> http://ppppp/pp, TestID -> Vector(ed811977, bd34bc09), gatling.http.cache.dns -> io.gatling.http.resolver.ShufflingNameResolver@53184a5c, gatling.http.ssl.sslContexts -> io.gatling.http.util.SslContexts@226d7a8c, gatling.http.referer -> http://PPPP/PP, TypeID -> Vector(00002600, 00000089), gatling.http.cookies -> CookieJar(Map(CookieKey(jsessionid,**,/) -> StoredCookie(JSESSIONID=yffgfffgdfgjhfghjfghfghfgjhfsss, path=/, HTTPOnly,true,false,1665496888776),data2)}

What actually required

[
{"name": "PerfTest1445", "id" : 13424, "typeid": 4566}, // values should be from list session attributes
{"name": "PerfTest4344", "id" : 10011, "typeid": 3423},
{"name": "PerfTest0055", "id" : 45633, "typeid": 5456}
]

Please suggest a solution to create Bulk Json request from Session attributes (Lists String) Saved from previous response
Please suggest a solution with an example ( even if there are other methods please provide a sample example)

Here at
.body(StringBody(session =>
s"“”${session(“data2”)}“”".stripMargin)).asJson) // data2 considered as text string Not as value.

// the entire previous session is taken as session value causing the error

How can I pass the value data2 from session function to .body(StringBody(session =>

Why do you string interpolation, then?

I don’t know your Json library, it looks like PlayJson which do not need to call toJson for east steps.
And I think you don’t have to use string interpolation for every values as well.

exec { session =>
  val PerfNamex = session("PerfName").as[List[String]]
  val PerfIDx = session("PerfID").as[List[String]]
  val TypeIDx = session("TypeID").as[List[String]] // <-- typo here?

  val data1 = PerfName.zip(PerfIDx).zip(TypeIDx).map {
    case ((name, id), typeId) =>
      Map(
        "name" -> name,
        "assetId" -> id,
        "typeId" -> typeId
      )
    }
  val data2 = Json.toJson(data2)

  session.set("data2", data2) // <-- do not forget to return the new session with the new value
}

Cheers

1 Like

Thank you very much, yes your solution solved the issue