Get byte array response to process and store a session attribute

Hello gatling user group and developers,

I want to send a request and receive a byte[] body response. Based on this byte[] response I want to extract a value (using protobuf) and the reuse this value in another resquest.

After some hours search, I cannot find a possibility to extract the http response body as a byte array:

val httpConfig = http
.baseURL(“http://www.whatever.com”)

val request = exec(http(“FirstRequest”)
.post("/message")
.body(new ByteArrayBody((session: Session) => getFirstRequest(session)))
.check(status.is(200), ???getByteResponse???))

val response = exec(http(“SecondRequest”)
.post("/message")
.body(new ByteArrayBody((session: Session) => getSecondRequest(session)))
.check(status.is(200), ???getByteResponse???))

val scn = scenario(“Request”).exec(request,response)

setUp(scn.inject(atOnce(1 user)))
.protocols(httpConfig)

Alternatively it would also be fine if I could set a value in getFirstRequest, that I can reuse in getSecondRequest:

private def getFirstRequest(session: Session): Array[Byte] = {

… setting a session attribute … (long)

… some protobuf stuff returning a byte array …

}

private def getSecondRequest(session: Session): Array[Byte] = {
var value= session(“value”).as[Long]

… some protobuf stuff using value from session and then returning byte array…

}

There’s a bodyString check that you can use with a transform step in order to get the bytes and deserialize them with Protobuf.
https://github.com/excilys/gatling/wiki/Checks#bodyString

https://github.com/excilys/gatling/wiki/Checks#transforming

FYI, I’ve just pushed a bodyBytes check too: https://github.com/excilys/gatling/issues/1836

Dear Stéphane,

thank you for your fast reply.

In my tries I also found the way to save the bodyString as a session variable. I guess since our server will response probably in UTF-8 I can transform it directly in the code, receiving it from the session.

Since I am really new to scala (Gatling is my firs try :)) is there a chance you can provide me a solution with transforming? I tried, but in my memories I must return a String, which at the end is the same as saveAs() the bodyString and catch it up in the second request.

Is there any chance that the a response object is available in the check method? Where you can just work with the “real” object instead of methods that already processed something.

Kind regards

Anton

Which version of Gatling do you use?

With current snapshot, it’s as simple as check(bodyBytes.saveAs(“bytes”))

With 2M3a, this would be check(bodyString.transform(.map(.getBytes(“UTF-8”))).saveAs(“bytes”))

Dear Stéphane,

is the M4 version available in maven?

I used the M3a maven archetype:

<gatling.version>2.0.0-M3a</gatling.version>
<gatling-highcharts.version>2.0.0-M3a</gatling-highcharts.version>

and have no idea what is the correct new version :slight_smile:

Kind regards

Anton

Honestly, I’m not convinced by this explanation. At all.
All UTF formats are reversible, meaning that the roundtrip bytes => char => bytes is lossless.
So, if the encoding you originally selected (see gatling.conf file) is UTF-8 (which is the default), you should be fine.

Then, current snapshot gives you bodyBytes check. You can grab it here: https://oss.sonatype.org/content/repositories/snapshots/io/gatling/highcharts/gatling-charts-highcharts/2.0.0-SNAPSHOT/

Dear Stéphane,

I got the 2.0.0-SNAPSHOT version by using this new repository:

oss-sonatype
oss-sonatype
https://oss.sonatype.org/content/repositories/snapshots/

true

and setting

<gatling.version>2.0.0-SNAPSHOT</gatling.version>
<gatling-highcharts.version>2.0.0-SNAPSHOT</gatling-highcharts.version>

With your new bodyBytes method it worked properly. Using the bodyString and mapping it to bytes using UTF-8 did not work for protobuf.

Thanks a lot!

Kind regards

Anton

Glad to hear!