Desidero utilizzare Spray per inviare a un server un modulo multipart. In particolare voglio pubblicare un'immagine.Spray Client Post Multipart
Quello che mi dà problemi è il Marshalling del file su Multipart. Anche se in Spray si parla di un Marshaller di default, non riesco a collegare i due insieme.
Attualmente sto usando Spray 1.0-M7 come non ho migrato a Scala 2.10, se l'esempio potrebbe essere compatibile con quel ramo, sarebbe meraviglioso.
Quello che ho attualmente è questo:
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))
E, naturalmente, ottengo un errore che dice:
For request 'POST /saveImageWithSpray' [Missing boundary header]
maggior parte degli esempi trovo utilizzare il contenuto (come [X]) direttiva per marshall , ma non sto usando Spray-routing, ho solo bisogno di eseguire il post utilizzando lo spray-client in un'applicazione costruita su un altro framework.
Grazie
EDIT
ho effettivamente riuscito a marshall in questo modo:
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
)
))))
Purtroppo questo non funziona ancora, i dati vengono sempre trasferiti ma il server cant trovare il file.
Una cattura Wireshark rivela quanto segue:
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
Questa è una cattura di una richiesta valida realizzato con un avanzato Riposo Cliente:
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
Sembra esserci carenza dell'attuale marshaller MultipartFormData, che non supporta i parametri 'filename'. Ho creato un problema per tenere traccia di questo: https://github.com/spray/spray/issues/327 – jrudolph
Quindi @dgrandes ... ha funzionato la correzione? Il nome del file spray è il tuo elemento form-data? Ho un problema simile, con 2 elementi di dati di forma: un file di testo/csv e una stringa di autorizzazione. Hai creato tu stesso la stringa di confine? Grazie – iyerland
Scusate @iyerland, ho finito per abbandonare la soluzione spray e implementato direttamente in Play! Struttura. Scusate! – dgrandes