1

I am new to scala ,Any help would be very much appreciated

I have a scala Map which has around 10 ids and 10 weights Map(id->id,weight->weight)

For post request in gatling , i would like to pass my request on basis of for loop Expected json: so for example , if there are 10 key values pair in my map - i would like to add 10 inner array in my final json string and there should be a single request.

myjson=

of[
     {
      "id":pis
      "weight":20.3
     }
  ]

Expected json=

of[
     {
      "id":pis
      "weight":20.3
     },
    {
      "id": 2nd value from for loop
      "weight":2nd value from for loop
     },
    {
      "id": 3rd value from for loop
      "weight":3rd value from for loop
     }

    ]

1 Answers1

1

Gatling provides Pebble Templates which can define and fill your body structure

There is a example how it works: https://gatling.io/2018/11/gatling-3-pebble-templating/

In your case body will be likes:

.body(PebbleStringBody(
      """{
        |  "of": [ {% for value in mapValues %}
        |    {
        |      "id": "{{value.id}}",
        |      "weight": "{{value.weight}}
        |    }{% if loop.last %}{% else %},{% endif %}
        |    {% endfor %}
        |  ]
        |}
        |""".stripMargin))

Feeder:

val mapValuesFeeder = Iterator.continually(Map("mapValues" -> List(
    Map("id" -> "1", "weight" -> "10"),
    Map("id" -> "2", "weight" -> "20"),
  )))

Amerousful
  • 2,292
  • 1
  • 12
  • 26