I'm trying out Akka-http and hopefully someone can shed light on a the following questions:
How does one create different routes based on the accept: header in the request? For example, i want one code path to handle "json" and one to handle "xml" requests (with default to "json" if header is missing)
In cases where I don't want the contentType to be inferred, how do i specify it? For example, in the code below I try to run the json through compactPrint() but this changes it to a string, hence "text/plain". I want to override that and tell the client it's still json.
My code is something like this;
...
path("api") {
get {
complete {
getStuff.map[ToResponseMarshallable] {
case Right(r) if r.isEmpty => List[String]().toJson.compactPrint
case Right(r) => r.toJson.compactPrint
case Left(e) => BadRequest -> e
}
}
}
}
...
The response in this case is text/plain, since compactPrint creates a string. criticism very welcome. ;)
Marshaller.oneOf
to compose different marshallers where each marshaller knows how to marshal to just one content-type. Have you seen the documentation about marshaller at doc.akka.io/docs/akka-stream-and-http-experimental/1.0/scala/…? – jrudolph