I am new to Gatlng and I am not able to find a good way to parameterize my json payload while doing a POST. I have to make as many unique POST calls to the rest api. So I have function which does that , look at the method
getSomeData() .
I use json4s to manipulate json payload. I am expecting that the json payload in my post request would be constructed each time a api call is made. But what seems to happen is , the method ``getSomeData is just called once and the data is updated in my json and the same data is used to run test which defeats my purpose. Is there any way to make this work. I am expecting that each request i make in through my scenario and setup, it gathers new data each time.
`
object GetPayloadData {}
def getSomeData(): String = {
// I am using Json4s to handle json.
val r = scala.util.Random
val lines = scala.io.Source.fromFile(“src/test/resources/bodies/SomeJson.json”).mkString
val Json = parse(lines).transformField {
case (“startDate”, date) => (“startDate”, LocalDate.now().toString() + “T00:00:00-07:00”)
case (“endDate”, endDate) => (“endDate”, LocalDate.now().plusDays(r.nextInt(100000)).toString() + “T00:00:00-07:00”)
case (“numOfAdults”, numOfAdults) => (“numOfAdults”, r.nextInt(100)) //I use scala utils random to get these values
case (“numOfWhatver”, numOfChildren) => (“numOfWhatever”, r.nextInt(100))
}
return (compact(render(Json)))
}
}
class SearchSimulation extends Simulation {
val Search_Api_Json = “SimpleSearch.json”
val searchPayLoad = new GetPayloadData
val httpConf = http
.acceptHeader(“text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8”) // Here are the common headers
.acceptEncodingHeader(“gzip, deflate”)
.acceptLanguageHeader(“en-US,en;q=0.5”)
.userAgentHeader(“Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0”)
val scn = scenario(“SearchDataLoading”) // A scenario is a chain of requests and pauses
.exec(http(“Search_Request”) // Here’s an example of a POST request
.post(“http://some/uri/gatling.search”)
.body(StringBody(searchPayLoad.getSomeData())).asJSON
.check(status.is(200)))
setUp(
scn.inject(
atOnceUsers(10), // 2
rampUsers(600) over (60 seconds) // 3
))
}
`