Open ktoso opened 8 years ago
Comment by 2beaucoup Wednesday Sep 09, 2015 at 07:45 GMT
You basically reimplemented content negotiation here, which akka-http already provides.
I think Marshaller.oneOf
is what you are looking for. Just bring a marshaller defined with that implicitly in scope and use the marshalling directives.
Issue by matthewadams Tuesday Sep 08, 2015 at 15:29 GMT Originally opened as https://github.com/akka/akka/issues/18425
NB: discussion at https://groups.google.com/forum/#!topic/akka-user/kpdsQsQVZJQ
I'm trying to allow a client to control (via the
Accept
header) theMediaType
used when a site is responding, provided that theAccept
header contains one of a collection of supportedMediaType
s. For example, I've written an endpoint to support application/json, text/xml, text/html and text/plain, and I'd like the client to be able to express its preference for which format it would like via theAccept
header, such thatAccept: text/xml
would cause the endpoint to useContent-Type: text/xml
in the response and send the response as xml. Same deal with application/json, text/html & text/plain.I saw the discussion at https://groups.google.com/forum/#!topic/akka-user/_054Z0G622w/discussion (where
respondWithMediaType
from Spray is considered an antipattern), so I wrote some code that I thought would work:When requesting the endpoint with an
Accept
header with valuetext/xml
, I get the following error:In an effort to reproduce the error with less code, I created the following class
Bug
(based on the above classMain
):Interestingly, in the presence of my custom
X-REPRO-BUG
header and the absence of anAccept
header, theBug
class works as expected. If, however, I add anAccept
header, I get an error response: