spray-client upload form with file

I have a next form:

   <form action="/upload" method="POST" enctype="multipart/form-data">
        <input type="text" name="name">
        <input type="file" name="file">
        <input type="submit" value="Upload image">
    </form>

I want to send request with name and with file

I use spray-client for this, when i send only file this work fine:

val file = "my-image.png"
val bis = new BufferedInputStream(new FileInputStream(file))
val bArray = Stream.continually(bis.read).takeWhile(-1 !=).map(_.toByte).toArray

val url = "http://example.com/upload"

val httpData = HttpData(bArray)
val httpEntity = HttpEntity(ContentTypes.`image/png`, httpData).asInstanceOf[HttpEntity.NonEmpty]
val formFile = FormFile("my-image", httpEntity)
val bodyPart = BodyPart(formFile, "my-image")
val req = Post(url, MultipartFormData(Map("spray-file" -> bodyPart)))

val pipeline = (addHeader("Content-Type", "multipart/form-data")
  ~> sendReceive
)

pipeline(req)

But how to send at the same time file and fields?

Answers


You're almost there. The only thing that's missing is adding some BodyParts to the Post request:

def headers(params: (String, String)*) =
  Seq(HttpHeaders.`Content-Disposition`("form-data", Map(params: _*)))


val formData = MultipartFormData(Seq(
  BodyPart(api_key, headers("name" -> "api_key")),
  BodyPart(api_secret, headers("name" -> "api_secret")),
  BodyPart(FormFile, "img")
))
Post = (url, formData)

Need Your Help

Issues with ReflectedDefinition attribute

visual-studio-2008 visual-studio-2010 f# compiler-errors

I have this code that does not compile anymore since I am on last F# CTP 1.9.7.8:

Accessing Row from Mysql DB by chaining query of foreign key from another table

php mysql database

I'm having difficulty trying to find the best way to get my results from a table. I want to get the targeted row from a table by one using the primary key from another using a foreign key.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.