Search code examples
gatling

Running and saving the response in repeat API gatling which is to be used in the next exec call


I want to call an API in the exec for n number of times. Each time, I want to save the response. All the responses would be collectively passed in an array in the body in my next exec call.

Example Let's say, I want to call upload Attachment API 5 times. The response of the API is in the following format.

{
  "attachmentId":"239092340ahds",
  "path":"abc/def"
}

After calling the above API 5 times, I want to use the response in the next exec call in the chain. The request body would look like this.

{
"data":[
{
  "attachmentId":"239092340ahds",
  "path":"abc/def"
},
{
  "attachmentId":"239092340ahds",
  "path":"abc/def"
},
{
  "attachmentId":"239092340ahds",
  "path":"abc/def"
},
{
  "attachmentId":"239092340ahds",
  "path":"abc/def"
},
{
  "attachmentId":"239092340ahds",
  "path":"abc/def"
}
]
}

How to achieve this in gatling?


Solution

  • I ended up using the transform function. I got a variable from session. Appended the response to that variable and then saved it in the same variable.

    .check(
          bodyString
            .transform((s, session) => {
              val response: String = session("response").as[String]
              if (!response.isBlank) {
                s + "," + response
              } else {
                s
              }
            })
            .saveAs("response")))