I'm trying to serve an image using akka-http, but keep getting error in the browser about MIME type:
Resource interpreted as Document but transferred with MIME type image/png: "http://localhost/banners/ci.png".
That's the server-side code:
pathPrefix("banners"){
get {
entity(as[HttpRequest]) { requestData =>
complete {
val fullPath = requestData.uri.path.toString match {
case _ => Paths.get(workingDirectory + requestData.uri.path.toString)
}
val contentType = getExtensions(fullPath.toString) match {
case "jpg" => ContentType(MediaTypes.`image/jpeg`)
case "png" => ContentType(MediaTypes.`image/png`)
case "html" => ContentTypes.`text/html(UTF-8)`
}
val h = RawHeader("Access-Control-Allow-Origin","*")
val byteArray = Files.readAllBytes(fullPath)
HttpResponse(OK, entity = HttpEntity(contentType,byteArray)).withHeaders(h)
}
}
}
} ~
Here is the response headers copied from chrome developer console:
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Server: akka-http/10.0.7
Date: Fri, 26 May 2017 13:08:04 GMT
Content-Type: image/png
Content-Length: 581
Any ideas how can I fix it ?