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
Acceptheader value and validates against it. So if route is returningapplication/jsonortext/plainand client acceptsimage/jpegthan spray will return406 Not Acceptable. If client will requestapplication/jsonortext/plainfrom 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.