Spray File Download: Unmarshalling MultiPartForm

I study spray using sprayer and sprayer-httpx (without spray routing) to accept downloaded files. I came up with the following:

def receive = { ... case HttpRequest(POST, Uri.Path("/upload"), _, entity, _) => object mp extends MultipartUnmarshallers mp.MultipartFormDataUnmarshaller(entity).foreach{ part => for{ fname <- part.fields.get("Filename").map(_.entity.asString) fbody <- part.fields.get("Filedata").map(_.entity.buffer) }{ println(fname+ ": " +fbody.length) } } sender ! HttpResponse(status = 200) ... } 

This works, but I think Unmarshaller Multipart is not intended to be used that way. Is there a more elegant way to do this?

+4
source share
1 answer

it may not be the most elegant, however it works

 val e = spray.httpx.unmarshalling.FormDataUnmarshallers.MultipartFormDataUnmarshaller.apply(request.entity) e.fold(error => { throw new IllegalArgumentException("could not unmarshall multipart form data") }, formdata => { for (part <- formdata.fields) { //do what you want with the parts } }) 
0
source

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


All Articles