从 Gatling 中的 csv 文件获取 json 请求正文

Get json request body from cvs file in Gatling

我有一个 csv 文件如下:

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

我想 Post 搜索标准作为请求正文中的 json 并准备了以下代码作为 gatling 场景,但它不起作用 - 我收到了 400 状态代码,因为 json 在正文中:

  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)

当我将 csv 文件中的 json 传递到正文语句中(如上所述)时,它起作用了:

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

您的文件不是正确的 CSV 文件,因为双引号是必须转义的保留字符。

来自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.

你应该有:

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