Spray Client Post Multipart

时间:2013-06-12 21:59:49

标签: scala multipartform-data spray spray-client

我想使用Spray将多部分表单发布到服务器。特别是我想张贴一张图片。

我遇到的麻烦是文件到多部分的编组。尽管在Spray中他们提到了默认的Marshaller,但我似乎无法将两者结合在一起。

我目前正在使用Spray 1.0-M7,因为我没有迁移到Scala 2.10,如果示例可以符合该分支,那将是非常棒的。

我现在拥有的是:

val bis = new BufferedInputStream(new FileInputStream(file))
val bArray = Stream.continually(bis.read).takeWhile(-1 !=).map(_.toByte).toArray
Logger.error("About to post with spray")
pipeline(Post("/saveImageWithSpray", bArray)) 

当然我得到一个错误说:

For request 'POST /saveImageWithSpray' [Missing boundary header]

我发现的大多数示例都使用内容(作为[X])指令进行编组,但我没有使用Spray-routing,我只需要在另一个框架上构建的应用程序中使用spray-client执行post。 / p>

由于

修改

我实际上设法像这样编组:

val pipeline = (
 addHeader("Content-Type", "multipart/form-data")
  ~> sendReceive(conduit)
)
val bis = new BufferedInputStream(new FileInputStream(file, "UTF-8"))
val bArray = Stream.continually(bis.read).takeWhile(-1 !=).map(_.toByte).toArray

Logger.error("About to post with spray "+bArray.length.toString)
pipeline(Post("/saveImageWithSpray", MultipartFormData(Map(
  "spray-file" -> BodyPart(
    HttpEntity(Some(HttpBody(ContentType(MediaTypes.`image/gif`), bArray))),
    HttpHeaders.`Content-Disposition`("form-data", Map("name" -> "spray-file","filename"->"Fuurin (Glass Wind Chime).gif"))::Nil
    )
  ))))

不幸的是,这仍然没有工作,数据正在转移,但服务器无法找到该文件。

wirehark捕获揭示了以下内容:

POST /saveImageWithSpray HTTP/1.1
Host: localhost:9000
User-Agent: spray-can/1.0-M7
Content-Type: multipart/form-data; boundary="oxz40rxXXQyDx+IUKcz7QYpJ"
Content-Length: 1725

--oxz40rxXXQyDx+IUKcz7QYpJ
Content-Disposition: form-data; name="spray-file"
Content-Disposition: form-data; name="spray-file"; filename="Fuurin (Glass Wind Chime).gif"
Content-Type: image/gif

GIF89a0.0..........
BINARY DATA
..P...L0..8.....X.....l..?...;
--oxz40rxXXQyDx+IUKcz7QYpJ--HTTP/1.1 500 Internal Server Error
Content-Type: text/plain; charset=utf-8
Content-Length: 25

File not found spray-file

这是对使用Advanced Rest Client进行的有效请求的捕获:

POST /saveImageWithSpray HTTP/1.1
Host: localhost:9000
Connection: keep-alive
Content-Length: 2573
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryuiIgwVg3rBQLFNGB
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

------WebKitFormBoundaryuiIgwVg3rBQLFNGB
Content-Disposition: form-data; name="spray-file"; filename="Gunsen (Fan) .gif"
Content-Type: image/gif

GIF89a0.0.........u.QQ..Z..z.wW[[[.
BINARY DATA
.....&...Z(.c.Q.....T.B7..S...!...p[...8."...;
------WebKitFormBoundaryuiIgwVg3rBQLFNGB--
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Content-Length: 24

File uploaded with spray

1 个答案:

答案 0 :(得分:1)

@grandes感谢您的回复。我使用newman rest客户端库(https://github.com/stackmob/newman)发送http请求,但必须手动创建MultipartFormData并且它有效。这将在不久的将来在纽曼修复。这是我用来手动构建多部分表单数据的链接:http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2