Message Sprayer

I want to use Spray to send a multi-page form to the server. In particular, I want to post an image.

I'm having problems with Marshalling File to Multipart. Despite the fact that they mention their default marshaller in Spray, I just can't connect them together.

I am currently using Spray 1.0-M7, since I havent ported to Scala 2.10, if this example can be compatible with this branch, that would be great.

I currently have:

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 message:

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

Most of the examples that I find use the content directive (like [X]) for the marshall, but I do not use Spray-routing, I just need to execute the post using the spray client in an application built on a different framework.

thanks

EDIT

I managed to do it as follows:

 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 still does not work, the data is transferred, but the server cannot find the file.

A wirehark capture shows 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 capturing a valid request made using the 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(.cQ....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 
+6
source share
1 answer

@grandes Thank you for responding. I used the newman rest client library ( https://github.com/stackmob/newman ) to send http requests, but I had to manually create MultipartFormData, and it worked. This will be fixed in a new person in the near future. Here is the link that I used to manually assemble data with several formats: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

+1
source

Source: https://habr.com/ru/post/947163/


All Articles