I have a problem with akka-http.
The Content-Type
headers default value is text/plain
. I am trying to set application/json
value like this:
val routes =
respondWithHeader(headers.`Content-Type`(ContentType(MediaTypes.`application/json`))) {
// ...
// all api routes
// ...
}
But this doesn't work. Content-Type
is still text/plain
.
UPD
I am NOT reading clients Content-Type
header. I am trying to send another from default Content-Type
header from server.
UPD2
For instance:
import JsonProtocol.entityFormat
//...
(get & path("api" / "entities")) {
complete(getAllEntities)
}
getAllEntities
returns a list of entities from DB
as Future[Entity]
.
Entity
is just a model:
case class Entity(foo: Int, bar: String)
EntityFormat
looks like:
object JsonProtocol extends spray.json.DefaultJsonProtocol {
implicit val entityFormat = jsonFormat2(Entity)
}
Finally cast Future
to ResponseMarshallable:
implicit def entity2ResponseMarshallable(entityF: Future[Entity]): ToResponseMarshallable =
entityF map (_.toJson.toString())