user9923760
user9923760

Reputation: 656

Gatling - String interpolation in Scala

I want to set the StringBody value in a variable:

exec(session => {session.set("searchBody", """{"productId":"${productID}","category":"${category}"}""")})

Variables ${productID} and ${category} exist. And I want to use the value as parameter of the function StringBody:

http("/products")           
  .post(appURL + "/search")
  .headers(jsonHeader)
  .body(StringBody("${searchBody}")).asJSON

But It doesn't work as expected. I am getting the following error: i.g.h.a.ResponseProcessor - Request '/products' failed:status.find.is(200), but actually found 403

Why are not the variables printed in the string for variable searchBody?

UPDATE:

I am trying that:

val search= exec(repeat(products.size, "n"){
    feed(products.circular)
        .uniformRandomSwitch( //5
            exec(session => {session.set("searchBody", """{"productId":"${productID}","minPrice":"${minPrice}"}""")}),
            exec(session => {session.set("searchBody", """{"productId":"${productID}","category":"${category}"}""")})
        )
    .exec(
        http("/products")           
            .post(appURL + "/search")
            .headers(jsonHeader)
            .body(StringBody("${searchBody}")).asJSON
            .check(status.is(200), responseTimeInMillis.lessThan("${expectedResponseTime}"))
    )
})

I want to send different type of requests balancing uniformly and I don't want to duplicate the exec part. The endpoint is always the same and only the body is different.

Upvotes: 1

Views: 2017

Answers (1)

Manoj Kumar Dhakad
Manoj Kumar Dhakad

Reputation: 1892

You need to make use of s string interpolation method of scala. Please find the below answer.

http("/products")           
  .post(appURL + "/search")
  .headers(jsonHeader)
  .body(StringBody(s"${searchBody}")).asJSON

Upvotes: 1

Related Questions