title: Sending a file that is available as inputstream rather than file
body: <p>In a legacy program that i am updating, i am receiving at some point a file uploaded by a user, this file need to be analyzed within an external service. </p>
  
  <p>I know how to send a file to a service using spray. In particular i know how to take a file from disk as the spray api is design as such</p>
  
  <p>In my current particular situation,  i only have an inputstream rather than a file. This is a file that is uploaded by the user to perform some analysis on it. That is, once i have the file, i would send it to an external service for analysis. However the spray API, especially http data only works with a file and not input stream, which is the datatype in which i have the data after the file is uploaded. I'm dealing with some legacy code. </p>
  
  <p>I was wondering how to deal with the fact that my file is at the moment an inputstream. I red somewhere that a solution could me to write to a temp file on disk and do the upload with it. But accessing the disk all the time for the matter sounds long to me. </p>
  
  <p>Is there any other way ?</p>
  
  <p>Below you can find a typical code i would write for the matter. However in this case, the file is only available to me in the form of an inputstream.</p>
  
  <p>Edit1</p>
  
  <pre><code>import context.dispatcher // execution context for futures below
  
        val file     = new File((getClass.getResource("/Health-Benefit-Plans.pdf")).toURI)
  
        val pipeline = addCredentials(BasicHttpCredentials("xxxxx", "xxxxxx")) ~&gt; sendReceive
  
        val payload  = MultipartFormData(Seq(BodyPart(file, "file", MediaTypes.`application/pdf`)))
  
        val request  = Post("xxxx/categorization?projectId=xxxxx&amp;language=en", payload)
        pipeline(request)
  </code></pre>
  
  <p>Edit2:</p>
  
  <p>Here is the code of BodyPart</p>
  
  <pre><code>    object BodyPart {
    @deprecated("Use a BodyPart.apply overload instead", "1.0/1.1/1.2")
    def forFile(fieldName: String, file: FormFile): BodyPart =
      apply(file, fieldName)
  
    def apply(file: File, fieldName: String): BodyPart = apply(file, fieldName, ContentTypes.`application/octet-stream`)
    def apply(file: File, fieldName: String, contentType: ContentType): BodyPart =
      apply(HttpEntity(contentType, HttpData(file)), fieldName, Map.empty.updated("filename", file.getName))
  
    def apply(formFile: FormFile, fieldName: String): BodyPart =
      formFile.name match {
        case Some(name) ⇒ apply(formFile.entity, fieldName, Map.empty.updated("filename", name))
        case None       ⇒ apply(formFile.entity, fieldName)
      }
  
    def apply(entity: HttpEntity, fieldName: String): BodyPart = apply(entity, fieldName, Map.empty[String, String])
    def apply(entity: HttpEntity, fieldName: String, parameters: Map[String, String]): BodyPart =
      BodyPart(entity, Seq(`Content-Disposition`("form-data", parameters.updated("name", fieldName))))
  }
  </code></pre>
  
  <p>As you can see there is nothing to deal with inputstream or something alike.</p>