Modify body response using ResponseWrapper

Hi ,

I am looking to find a way to modify the body response, i’ve found this : https://github.com/excilys/gatling/issues/1605

I want access to the bodyString, this example would have been perfect :

import io.gatling.http.response.DelegatingReponse
import com.ning.http.util.Base64

.transformResponse {
    case response: Response => new DelegatingReponse(response) {
         override def bodyString = {
             val decodedBytes = Base64.decode(response.bodyString)
             new String(decodedBytes, "UTF-8")
         }
    }
}

But DelegatingReponse has been updated into ReponseWrapper and we don’t have direct access to the body anymore.

Thanks,
David.

I just added an example: https://github.com/excilys/gatling/blob/master/src/sphinx/http/http_request.rst#response-processors

Thanks, it’s working fine.

HI,

A quick question more a scala one, but how do i externalize my inner code to transform the response in a reusable function ?

Here is the code :

.transformResponse { case response if response.isReceived => new ReponseWrapper(response) { override val body = { if (response.body.string.startsWith("{}&&")) { StringResponseBody(response.body.string.substring(4), UTF_8) } else { StringResponseBody(response.body.string, UTF_8) } } } }

I tried something like that; :
val formatResponse = { case response if response.isReceived => new ReponseWrapper(response) { override val body = { if (response.body.string.startsWith("{}&&")) { StringResponseBody(response.body.string.substring(4), UTF_8) } else { StringResponseBody(response.body.string, UTF_8) } } } }

But i’m surely missing something about it.;

Sorry for this question…

You have to give the Scala compiler a hint about response type:

  • either define formatResponse type: PartialFunction[Response, Body]
  • or define response type: Response

Thanks Stephane,

Hi did it like this as you suggested and it’s working fine.

val formatResponse : PartialFunction[io.gatling.http.response.Response,io.gatling.http.response.Response] = { case response if response.isReceived => new ReponseWrapper(response) { override val body = { if (response.body.string.startsWith("{}&&")) { StringResponseBody(response.body.string.substring(4), UTF_8) } else { StringResponseBody(response.body.string, UTF_8) } } } }

I would have gone the other way :slight_smile:

import io.gatling.http.response.Response
val formatResponse = {

case response: Response if response.isReceived =>
new ReponseWrapper(response) {
override val body = {
if (response.body.string.startsWith("{}&&")) {
StringResponseBody(response.body.string.substring(4), UTF_8)
} else {
StringResponseBody(response.body.string, UTF_8)
}
}
}

}

This way won’t work :

`
val formatResponse = {
case response: io.gatling.http.response.Response if response.isReceived =>
new ReponseWrapper(response) {
override val body = {
if (response.body.string.startsWith("{}&&")) {
StringResponseBody(response.body.string.substring(4), UTF_8)
} else {
StringResponseBody(response.body.string, UTF_8)
}
}
}

`

So i’m gonna stick with the previous one :slight_smile: