Not able to send unique requests.

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
))

}
`

If I’m not mistaken, the answer to your question was posted a few days ago:
https://groups.google.com/d/msg/gatling/aSw78SXb-Tk/-SmTeKHtEAAJ

yes it does look similar. But i am trying to build the JSON body as string and passing it through as payload. But in the example
:

.body(StringBody(_ => “”"{“name”:"""" + Name.first_name.toLowerCase + “”"",“age”:""" + ThreadLocalRandom.current().nextInt(10, 80) + “”"}"""))

and I am trying to do it as :

.body(StringBody(searchPayLoad.getSomeData())).asJSON..

Please try simply transforming your call to lambda:

`
.body(StringBody(_ => searchPayLoad.getSomeData())).asJSON

`

Cheers,
Adam

@adam : Thanks.

For some reason , i tried that before posting it to this group. And I must be tired enough I did not see it working , until I checked back again. I am new to scala and gatling; why is this not very evident from the documentation or is it not the way one should approach the tests?