Hi David,
On Tue, May 6, 2014 at 1:39 PM, David Pérez
<
david.pere...@gmail.com> wrote:
> Now the problem I'm having is this:
>
> case class Icon(bytes: Array[Byte], tipo: MediaType)
>
> def icon(idPortal: String): Option[Icon]
>
> val myRoute = path("icon") {
> get {
> icon(idPortal) match {
> case None => complete(NotFound)
> case Some(Icon(bytes, mime)) =>
> respondWithMediaType(mime) {
> complete(bytes)
> }
> }
> }
> }
>
> In my test function, "bytes" is Array[Byte](1, 2, 3) and "mime" is
> `image/jpeg` and is being serialized as [ 1, 2, 3], even though I told it to
> use custom mime. I suspect that is trying to use json.
Yes, using respondWithMediaType doesn't change the behavior of
Marshalling in any way. You have basically two options: Either you
implement a custom `Marshaller[Icon]` (see Marshaller.of as an
example), this will then automatically support "proper" content-type
negotiation, meaning that the right error will be delivered if the
client isn't accepting the content-type you are providing. Or, if you
don't care for proper error reporting you can also just complete the
request with an HttpEntity with `HttpEntity(contentType, bytes)`.
> Marshalling in Spray is powerful, but a little complicated for newbies.
Actually, marshalling and Content-Type negotiation is also complicated
in HTTP so trying to hide at least some of the complexity should be a
good thing. Still, you may be right that things are not as easy to do
and as understandable as they could be.