scalaperformance-testinggatlingscala-gatling

How to update the content of JSON file before using it as request body


{
  "aggregateResults": true,
  "targetTimeSeries": "finalForecast",
  "filter": {
    "id": "f3456789sdsd779sds4488"
  }
}

This the content of my JSON file, I want to change the value of id before sending this JSON as body in a POST request for my Gatling script.

Since this is small JSON, currently I am passing it to StringBody and replacing the dynamic part to achieve this successfully but this method will not be useful for long JSON responses with which I have to deal with in future.


Solution

  • You can embed gatling expressions in your JSON.

    Assuming your desired id is in a session variable called "id" you could have your file like this:

    {
      "aggregateResults": true,
      "targetTimeSeries": "finalForecast",
      "filter": {
        "id": "${id}"
      }
    }
    

    And then use an ELFileBody instead of a RawFileBody to supply the file.