Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spray Client Post Multipart

I want to use Spray to post to a server a multipart form. In particular I want to post an Image.

What Im having trouble with is the Marshalling of the File to Multipart. Even though in Spray they mention its a default Marshaller, I just cant seem to plug the two together.

Im currently using Spray 1.0-M7 as I havent migrated to Scala 2.10, if the example could be compliant with that branch, that would be wonderful.

What I currently have is this:

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)) 

And of course I get an error saying:

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

Most examples I find use the content(as[X]) directive to marshall, yet I am not using Spray-routing, I just need to execute the post using the spray-client in an application built on another framework.

Thanks

EDIT

I have actually managed to marshall it like this:

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
    )
  ))))

Unfortunately this is not working still, the data is getting transferred but the server cant find the file.

A wireshark capture reveals the following:

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

This is a capture of a valid request made with a 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
like image 322
dgrandes Avatar asked Jun 12 '13 21:06

dgrandes


1 Answers

@grandes Thanks for responding. I used the newman rest client library (https://github.com/stackmob/newman) to send http requests, but had to create the MultipartFormData manually and it worked. This will be fixed in newman in the near future. Here's the link that I used to manually build the multipart form data: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

like image 151
iyerland Avatar answered Oct 14 '22 03:10

iyerland