Search code examples
scalagatlingscala-gatling

Gatling how to send json body to avoid unmarshal error


I'm trying to send a JSON body to my API using Gatling, but I keep getting error 400 because it cannot unmarshal the body I'm sending:

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

In the Gatling logs I can read that I'm sending this kind of request:

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

I don't think I'm creating the correct body, Is there a way to send only { "productId": XXXXX, "qty": 1 , "storeId": XXXXXX} as body?

I might have put the contentType in the wrong way, what is the right order?.


Solution

  • Are you sure product1 and storeId are numbers and not Strings? Otherwise, they must be wrapped with double quotes, which they currently aren't.