dgr*_*des 6 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))
Run Code Online (Sandbox Code Playgroud)
当然,我得到一个错误说:
For request 'POST /saveImageWithSpray' [Missing boundary header]
Run Code Online (Sandbox Code Playgroud)
我找到的大多数示例使用内容(作为[X])指令进行编组,但我没有使用Spray-routing,我只需要在另一个框架上构建的应用程序中使用spray-client执行post.
谢谢
编辑
我实际上设法像这样编组:
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
)
))))
Run Code Online (Sandbox Code Playgroud)
不幸的是,这仍然没有工作,数据正在转移,但服务器无法找到该文件.
wireshark捕获显示以下内容:
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
Run Code Online (Sandbox Code Playgroud)
这是对使用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
Run Code Online (Sandbox Code Playgroud)
@grandes 感谢您的回复。我使用 newman Rest 客户端库(https://github.com/stackmob/newman)发送 http 请求,但必须手动创建 MultipartFormData 并且它可以工作。这将在不久的将来在纽曼中得到修复。这是我用来手动构建多部分表单数据的链接:http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2
| 归档时间: |
|
| 查看次数: |
3108 次 |
| 最近记录: |