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)
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)}```
Expected
[
{"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
CodePudding user response:
you need to set the session value session.set("data2", data2)
and convert the data2 as string
.body(StringBody(session =>
s"""${session("data2").as[String]}""".stripMargin)).asJson)