I am trying to replace a Jetty-based back-end by a pure spray-can + spray-routing one.
The front-end posts JSON content using the text/json media type. I've never had any problems with this with Jetty. I have just realized that it is not a standard type thanks to spray, which only accepts the correct and standard application/json media type.
Is there any easy way to map one to the other at the server side? I would really like to avoid having to introduce an ad-hoc release of the client side of the application to deal with this. Of course, I will make the change for the next scheduled release, but for now I need a "quick and dirty" fix.
I have tried changing the header from text/json to application/json using the following function:
def correctJsonHeaders(req:spray.http.HttpRequest) = {
val tweakedHeaders = req.headers.map{ hdr =>
if(hdr.name == "Content-Type" && hdr.value == "text/json")
`Content-Type`(`application/json`)
else
hdr
}
req.copy(headers = tweakedHeaders)
}
in my route directive, like so:
path("route"){
mapRequest(correctJsonHeaders){
post{
respondWithMediaType(`application/json`) {
handleWith{ x:TypeThatUnmarshallsFromJson =>
bizLogicReturningAJsonString(x)
}
}
}
}
}
Although the header is correctly changed, I still get a 415 error (which goes away if I change the media type to application/json at the client)