How to use Array length to iterate/loop a post request and pass sequential array indexed value ? in scala-gatling

How to use Array length to loop a post request with passing sequential array indexed value ?

      .formParam("testvalue", "Testalert")
      .check(jsonPath("$[*].id").findAll.saveAs("TestID"))) // datatype : vector string / single dimensional array

I have to iterate the below request with testID.length and pass the array indexed value (TestID[i] to the json page

repeat(testID.length) {
        .post(TestUrl + "/graphql")
        .body(ElFileBody("testpage/testPageView.json")) // pass value TestID[i] , i++ at each iteration

for example :
testPageView.json file contains
{"query":"query{api{test(limit:${TestID[i]}){edges{node{id type{role}}}}\"}

Kindly suggest a solution

Hi @RudraGanesh,

import static io.gatling.javaapi.core.CoreDsl.*;
import static io.gatling.javaapi.http.HttpDsl.*;

import io.gatling.javaapi.core.*;
import io.gatling.javaapi.http.*;

public class ArraySimulation extends Simulation {

    HttpProtocolBuilder httpProtocol =

    ScenarioBuilder scn =
                                    .body(StringBody("{\"values\":[{\"id\":\"one\"}, {\"id\":\"two\"}, {\"id\":\"three\"}]}"))
                                    .check(jsonPath("$.json.values[*].id").findAll().saveAs("TestID")) // 1
                    ).foreach("#{TestID}", "currentID").on( // 2
                                    http("Get with the value")
                                            .queryParam("id", "#{currentID}") // 3


Note: Here is a real SSCCE, you can run it without issue yourself.

  1. I had to change the jsonPath because postman-echo changes it.
  2. foreach is usually the way to do. Note that the “currentID” is the name of the session variable to store for each value in the list.
  3. I use the currentID value as you can use it in your testpage/testPageView.json file



@sbrevet Thank you very much
Yes, your solution worked :slight_smile: