title: Akka HTTP: ByteString as a file payload in a form-data request
body: <p>In one of my <a href="https://stackoverflow.com/questions/50046266/how-to-represent-a-form-data-request-using-akka-http">previous questions</a>, I asked how to represent a form-data request using Akka HTTP? According to answers I created a working sample, but faced a "scaleability" issue - when the number of form-data requests is high, I need to handle a lot of files in the file system.</p>
  
  <p>I'm curious, is it possible to send <code>ByteString</code> as a file payload in a form-data request?</p>
  
  <pre class="lang-scala prettyprint-override"><code>case class FBSingleChunkUpload(accessToken: String, 
          sessionId: String,
          from: Long, 
          to: Long, 
          file: ByteString) //this property is received from S3 as array of bytes
  </code></pre>
  
  <p>I created a following sample:</p>
  
  <pre class="lang-scala prettyprint-override"><code>def defaultEntity(content: String) =
    HttpEntity.Default(
      ContentTypes.`text/plain(UTF-8)`,
      content.length, Source(ByteString(content) :: Nil)
    )
  
  def chunkEntity(chunk: ByteString) =
    HttpEntity.Strict(
      ContentType(MediaTypes.`application/octet-stream`),
      chunk
    )
  
  val formData = Multipart.FormData(
    Source(
      Multipart.FormData.BodyPart("access_token", defaultEntity(upload.fbUploadSession.fbId.accessToken)) ::
      Multipart.FormData.BodyPart("upload_phase", defaultEntity("transfer")) ::
      Multipart.FormData.BodyPart("start_offset", defaultEntity(upload.fbUploadSession.from.toString)) ::
      Multipart.FormData.BodyPart("upload_session_id", defaultEntity(upload.fbUploadSession.uploadSessionId)) ::
      Multipart.FormData.BodyPart("video_file_chunk", chunkEntity(upload.chunk)) :: Nil
    )
  )
  val req = HttpRequest(
    HttpMethods.POST,
    s"/v2.3/${upload.fbUploadSession.fbId.pageId}/videos",
    Nil,
    formData.toEntity()
  )
  </code></pre>
  
  <p>In this case Facebook sends me back a message:</p>
  
  <blockquote>
    <p>Your video upload timed out before it could be completed. This is
    probably because of a slow network connection or because the video
    you're trying to upload is too large</p>
  </blockquote>
  
  <p>But if I send the same <code>ByteString</code> as a <code>File</code> it works fine.</p>
  
  <p>What could be the reason of this? I already tried to use <code>MediaTypes.multipart/form-data</code> in <code>chunkEntity</code> but it behaves in the same way.</p>