加特林如何发送 json 正文以避免解组错误

Gatling how to send json body to avoid unmarshal error

我正在尝试使用 Gatling 将 JSON 正文发送到我的 API,但我一直收到错误 400,因为它无法解组我发送的正文:

  private val authHeaders = Map(
    "Accept" -> "application/json, text/javascript, */*; q=0.01",
    "Authorization" -> "Bearer ${access_token}"
  )
 
 var requestBody: Body with (Expression[String]) =StringBody("{ \"productId\":  " + 
 product1 + ", \"qty\": "+ 1 +" , \"storeId\": "+ storeId + "}")

 var addToCart: ChainBuilder = exec(http("addToCart")
    .post(addToCartUrl)
    .headers(authHeaders)
    .body(requestBody)
    .check(bodyString.saveAs("body"))
  )

在 Gatling 日志中我可以读到我正在发送这种请求:

HTTP request:
POST ....
headers:
    Accept: application/json, text/javascript, */*; q=0.01
    Authorization: Bearer ....
    cookie: ROUTE=....
    host: .....
    content-length: 53
cookies:
    ROUTE=...., path=/, secure, HTTPOnly, SameSite=None
body:StringChunksRequestBody{contentType='null', charset=UTF-8, content={ "productId":  XXXX, "qty": 1 , "storeId": XXXX}}

我认为我没有创建正确的正文,有没有办法只发送 { "productId": XXXXX, "qty": 1 , "storeId": XXXXXX} 作为正文?

我可能把 contentType 放错了,正确的顺序是什么?

您确定 product1storeId 是数字而不是字符串吗?否则,它们必须用双引号括起来,目前还没有。