# Creating an HTTP request using a fast and efficient InputStream

**URL:** <https://community.gatling.io/t/creating-an-http-request-using-a-fast-and-efficient-inputstream/3342>\
**Category:** Gatling (Open-Source)\
**Created:** [October 26, 2016, 11:23pm UTC](https://community.gatling.io/t/creating-an-http-request-using-a-fast-and-efficient-inputstream/3342 "2016-10-26T23:23:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stefano\_Sgorlon](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Stefano\_Sgorlon](https://community.gatling.io/u/Stefano_Sgorlon)\
**Post date:** [October 26, 2016, 11:23pm UTC](https://community.gatling.io/t/creating-an-http-request-using-a-fast-and-efficient-inputstream/3342/1 "2016-10-26T23:23:42Z")

</div>

```auto

Hi,

I am using __*Gatling*__ 2.2.2
I have been reading this page: http://gatling.io/docs/2.2.2/http/http_request.html

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 
Stefano 

```

```auto

```

```auto
// 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
      }
    }

    dummyInputStream
  }

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

  def apiRequest(httpRequest: HttpRequestBuilder): HttpRequestBuilder = 
    httpRequest.check(status.saveAs("statusCode"))

  val is: InputStream = StreamUtils.generateFakeInputStream(size)

  val httpRequest = apiRequest(
    http(s"POST with Upload Bytes $size")
      .post(postUploadUrl)
      .headers(headers)
      .body(InputStreamBody(is))
      .check(status.is(200)))

  // is.close()

  httpRequest
}

```

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [October 27, 2016, 7:57am UTC](https://community.gatling.io/t/creating-an-http-request-using-a-fast-and-efficient-inputstream/3342/2 "2016-10-27T07:57:18Z")

</div>

Have you tried with current snapshot?

---

<div class="post-metadata">

**Author:** ![Stefano\_Sgorlon](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Stefano\_Sgorlon](https://community.gatling.io/u/Stefano_Sgorlon)\
**Post date:** [October 27, 2016, 2:13pm UTC](https://community.gatling.io/t/creating-an-http-request-using-a-fast-and-efficient-inputstream/3342/3 "2016-10-27T14:13:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dylan\_wilder](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@dylan\_wilder](https://community.gatling.io/u/dylan_wilder)\
**Post date:** [February 8, 2017, 6:45pm UTC](https://community.gatling.io/t/creating-an-http-request-using-a-fast-and-efficient-inputstream/3342/4 "2017-02-08T18:45:11Z")

</div>

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

in my exec() i have:

---

<div class="post-metadata">

**Author:** ![dylan\_wilder](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@dylan\_wilder](https://community.gatling.io/u/dylan_wilder)\
**Post date:** [February 8, 2017, 6:54pm UTC](https://community.gatling.io/t/creating-an-http-request-using-a-fast-and-efficient-inputstream/3342/5 "2017-02-08T18:54:53Z")

</div>

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