In spray I would like to respond with different content-types, depending on the given Accept
header. I've seen a couple of suggestions in the question by rompetroll, but I would like to hear if there are any canonical way of doing it (i. e. simple or already implemented).
In essence what I imagine should happen is something like:
path("somepath") {
get {
// Find whatever we would like to return (lazily)
...
// Marshall resource and complete depending on the `Accept` header
...
}
}
Thanks in advance.
Spray is actually looking into the Accept
header value and validates against it. So if route is returning application/json
or text/plain
and client accepts image/jpeg
than spray will return 406 Not Acceptable
. If client will request application/json
ortext/plain
from this route than he will receive repsonse with matching Content-Type.
The main trick here is to use correct marshallers for return objects. You can read more about marshalling here.
Also you can override MediaType with respondWithMediaType directive, but I think it is better to use correct marshallers.