Creating an HTTP request using a fast and efficient InputStream


I am using __*Gatling*__ 2.2.2
I have been reading this page:

I am trying to create a Gatling simulation that uses a __*HttpRequestBuilder*__ with an __*InputStreamBody*__
My goal is to create a very fast and efficient HTTP request that generates on-the-fly N (N around 1-2 GB) random bytes to be sent via __*POST*__ method.
It is not important how the bytes are generated, but I don't want to store them in memory or in a resource/file.
And everything needs to be fast, using a stream. 
I discarded the ideas of using __*RawFileBody*__ or __*ByteArrayBody*__ or *StringBody*, and I thought to pass an __*InputStream*__ to __*InputStreamBody*__

Below you can find my current code, which seems fine. But for some reasons it does not work correctly over 50kB.
Could you check if you spot anything wrong? Do I need to specify special headers in the request? What are your suggestions? 

If I use __*RawFileBody*__ I can upload the without problems and quickly files over 1 GB. But as I said before I do not want to store files, I would like to be able to generate the bytes using a stream.

Thanks a lot 

// maximum Int value = 2147483647
def postInputStream(size: Int) = {
  val postUploadUrl =  "SOME_HTTP_URL"

  val DUMMY_BYTE: Byte = 'A'.toByte

  def generateFakeInputStream(n: Int): InputStream = {
    require(n > 0)

    val dummyInputStream = new InputStream {
      var counter: Int = 0

      def resetAndCloseStream: Int = {
        counter = 0
        -1 // end of stream

      override def read(): Int = {
        counter += 1
        if (counter <= n) DUMMY_BYTE
        else resetAndCloseStream


  val headers = Map(
    "Content-Length" -> String.valueOf(size),
    "Content-Type" -> "text/plain"

  def apiRequest(httpRequest: HttpRequestBuilder): HttpRequestBuilder = 

  val is: InputStream = StreamUtils.generateFakeInputStream(size)

  val httpRequest = apiRequest(
    http(s"POST with Upload Bytes $size")

  // is.close()


Have you tried with current snapshot?

I am trying now with 2.3.0-SNAPSHOT, but I do not see differences

Hi I am trying to do something similar but in a different way.

in my exec() i have:

Actually I just tried the above posted method. Same issue. I get an empty body.