How to handle List session attributes and for loop in PebbleStringBody to create a bulk Json request. Gatling-Scala

How to handle List session attributes and for loop in PebbleStringBody to create a bulk Json request.
Gatling-Scala

  .exec(http("Find Perf")
      .get("/Perf")
      .queryParam("PerfId", PerfId)
      .check(jsonPath("$.results[*].id").findAll.saveAs("PerfTestID"))
      .check(jsonPath("$.results[*].name").findAll.saveAs("PerfName"))
      .check(jsonPath("$.results").findAll.saveAs("TotalPerfdata"))
      .check(status.is(200))
    )
    .exec(http("Perf_Execution")
      .post("/Perf/bulk")
      .header("Content-Type", "application/json")
      .body(PebbleStringBody(
        """[
           | {% for t in range(1,2) %}// I tried {{PerfTestID}} in range but it throws syntax error
           |  {
           |    "id": "{{PerfTestID}}",
           |     "name":{{PerfName}}",
           |    "value": {}
           |  }
           |  {% if loop.last %}
           |  {% else %},{% endif %}
           |  {% endfor %}
           |  ]""".stripMargin)).asJson)
  setUp(scn.inject(atOnceUsers(nbUsers)))
  .protocols(httpProtocol);

Error Result :

body:StringRequestBody{charset=UTF-8, content=[
  {
    "id": "[ed811977-0992a, bd34bc09-e748]", 
     "name": "[Perftest1,Perftest2]"
    "value": {}
  }
  ,  
  {
    "id": "[ed811977-0992a, bd34bc09-e748]", // Value Identical at for loop iteration
     "name": "[Perftest1,Perftest2]"
    "value": {}
  }        ]}

Issues here :

  1. Cannot dynamically assign value to for loop
  2. do not know how List ( Session attributes) will work in Pebble string
  3. after the loop it gives same value at each iteration
    – causing 400 error

Expected result :

body:StringRequestBody{charset=UTF-8, content=[
  {
    "id": "ed811977-0992a",
     "name": "Perftest1"
    "value": {}
  }  ,  
  {
    "id": "bd34bc09-e748",
     "name": "Perftest2"
    "value": {}
  }        ]}

Please suggest a solution with example

As described in the Pebble documentation:
https://pebbletemplates.io/wiki/guide/basic-usage/#variables

.body(PebbleStringBody(
        """[
           | {% for t in range(1,2) %}// I tried {{PerfTestID}} in range but it throws syntax error
           |  {
           |    "id": "{{PerfTestID[t]}}", // <--- can index list
           |     "name":{{PerfName[t]}}",
           |    "value": {}
           |  }
           |  {% if loop.last %}
           |  {% else %},{% endif %}
           |  {% endfor %}
           |  ]""".stripMargin))

Cheers

1 Like

@sbrevet
thanks for responding
but considering the session attribute as list and indexing it inside the for loop is taking only the first value.

the result : {
“id”: “ed811977-0992a”,
“name”: “Perftest1”
“value”: {}
} ,
{
“id”: “ed811977-0992a”,
“name”: “Perftest1”
“value”: {}
}

but expected result is
{
“id”: “ed811977-0992a”,
“name”: “Perftest1”
“value”: {}
} ,
{
“id”: “bd34bc09-e748”,
“name”: “Perftest2”
“value”: {}
} ]}

how to handle the for loop here so the List/array values are taken in with indexing sequentially