Skip to content

Instantly share code, notes, and snippets.

@priyanshus
Last active January 5, 2021 07:33
Show Gist options
  • Save priyanshus/bde59419e160b31f0b38df09a8121f29 to your computer and use it in GitHub Desktop.
Save priyanshus/bde59419e160b31f0b38df09a8121f29 to your computer and use it in GitHub Desktop.
Gatling Request Body Processor Use Case

Gatling provides Request Body Processor processRequestBody() to process the request body before it sent to over wire.

There are two built-in processors available but one can have their own custom processor to modify the body content before sending it to server.

Below code demonstrates an use case where I am using a text file to fetch the request body as .body(ElFileBody("payload.txt")) and modifying the _placeholder with some computer name before sending.

package simulation
import io.gatling.core.Predef.Simulation
import io.gatling.core.body.{Body, ByteArrayBody, CompositeByteArrayBody, InputStreamBody, PebbleBody, RawFileBody, ResourceAndCachedBytes, StringBody}
import io.gatling.core.config.GatlingConfiguration
abstract class ConfigurableSimulation extends Simulation {
def processPayload(implicit configuration: GatlingConfiguration) : Body => StringBody =
(body: Body) => {
val processedBodyBytes = body match {
case b: CompositeByteArrayBody => b.asStream.map(inputStream => {
val actualPayload = new String(inputStream.readAllBytes())
actualPayload.replace("_placeholder", "some computer name")
})
}
StringBody(processedBodyBytes)
}
}
name=_placeholder&introduced=2020-01-01&discontinued=&company=1
package simulation
import io.gatling.core.Predef._
import io.gatling.http.Predef._
class SampleSimulation extends ConfigurableSimulation {
val httpProtocol = http
.baseUrl("https://httpdump.io")
.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
.userAgentHeader("Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0")
val dumpRequestHttp = http("Dump Request Http")
.post("/bq2lj")
.body(ElFileBody("payload.txt"))
.processRequestBody(processPayload)
val scn = scenario("Dump Request")
.exec(dumpRequestHttp)
setUp {
scn.inject(atOnceUsers(1))
}.protocols(httpProtocol)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment