Sunday, February 7, 2016

Gatling read json response and store as list in session

Consider a REST end point /persons which returns JSON response of array of Person object. The Person object having property named id. You want to store all the returned id in personIds list in session to use it further. The REST response is like this -
[
  {
    "id": 1,
    "name": "Jack",
  },
  {
    "id": 2,
    "name": "Jill"
  }
]
  val getAllPersons= http("Get all persons")
    .get("/persons")
    .check(status.is(200), jsonPath("$..id").findAll.optional.saveAs("personIds"))

The above code will extract id values and store as list in personIds.
If REST response returns empty list then the above code will fail with error -
jsonPath($..id).findAll.exists, found nothing

Using optional in the chain helps avert the error. Only if the findAll returns any thing the saveAs will execute and will work without failure.

No comments:

Post a Comment