Home > Blockchain >  Gatling how to send json body to avoid unmarshal error
Gatling how to send json body to avoid unmarshal error

Time:02-03

I'm trying to send a json body to my Api using Gatling, but I keep getting error 400 because it cannot unmarshall 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?. Thanks for your collaboration!

CodePudding user response:

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

  •  Tags:  
  • Related