Search code examples
jsonscalapostgatling

Get json request body from cvs file in Gatling


I have a csv file as followed:

Id,searchCriterion
18817,"{"basicSearchCriteria":{"name":{"text":"Kas"}}}"

I want to Post search criterion as a json in request body and prepared the following code as gatling scenario, but it does not work - I received 400 status code because of incorrect json in body:

  val feeder = csv("search.csv")
  
  object SearchWithCriteria
  {
    var request =
          feed(feeder)
          .exec(
             http("POST with criteria page 1")
            .post("api/resources?pageNumber=1&pageSize=10&id=${Id}")
            .body(StringBody("""${searchCriterion}"""))
                  .check(status.is(200))
                  )
  }

  val basicSearch = scenario("Basic search (no search criteria)").exec(SearchWithCriteria.request)

  setUp(
    basicSearch.inject(rampUsers(1) during (1 seconds))
  ).protocols(httpProtocol)

When I past the json from csv file into body statement (as above) it works:

.body(StringBody("""{"basicSearchCriteria":{"name":{"text":"Kas"}}}"""))

Solution

  • Your file is not correct CSV because double quotes are reserved characters that have to be escaped.

    From rfc4180:

    If fields are not enclosed with double quotes, then double quotes may not appear inside the fields.

    If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote.

    You should have:

    Id,searchCriterion
    18817,"{""basicSearchCriteria"":{""name"":{""text"":""Kas""}}}"