scalagatlingscala-gatlinggatling-plugin

Gatling request body as bytearray


val scn = scenario("gatling test").
    feed(circularfeeder.circular)
    .exec(http("request")
      .post(endpoint)
      .header("Content-Type", "application/json")
      .header("Accept-Encoding", "charset=UTF-8")
      .body(StringBody("""${request}"""))
      .check(status.is(200))

The above code is used to send every circularfeeder data as a string to the body of the request. Suppose if i want to send as byte array instead of string in the body how do I modify the line .body(StringBody("""${request}"""))

The code .body(ByteArrayBody(getBytesData("""${request}"""))) does not work. Any advise?


Solution

  • Option 1

    Call getBytes method on request string and pass that to ByteArrayBody

    .body(ByteArrayBody { session => 
      session("request")
        .validate[String]
        .map(s => s.getBytes(StandardCharsets.UTF_8)) 
    })
    

    Option 2

    Convert the raw data you got from your feeder.

    val circularfeeder = csv("myFile.csv").convert {
      case ("request", string) => string.getBytes(StandardCharsets.UTF_8)
    }
    ...
    .body(ByteArrayBody("${request}"))