0

Is any version of Scalatra capable of serving images with the correct Content-Type header? I can't seem to suppress the automatic addition of charset.

Example of returning a File:

class MyServlet extends org.scalatra.ScalatraServlet {
  get("/") {
    contentType = "image/png"
    new java.io.File("/tmp/test.png")
  }
}
$ curl --head localhost:8080
HTTP/1.1 200 OK
Date: Thu, 01 Aug 2019 21:43:28 GMT
Content-Type: image/png;charset=utf-8
Transfer-Encoding: chunked
Server: Jetty(9.4.8.v20171121)

Example of returning an InputStream:

class MyServlet extends org.scalatra.ScalatraServlet {
  get("/") {
    contentType = "image/png"
    new java.io.FileInputStream(new java.io.File("/tmp/test.png"))
  }
}
$ curl --head localhost:8080
HTTP/1.1 200 OK
Date: Thu, 01 Aug 2019 21:49:52 GMT
Content-Type: image/png;charset=utf-8
Content-Length: 2881
Server: Jetty(9.4.8.v20171121)
earldouglas
  • 13,265
  • 5
  • 41
  • 50

1 Answers1

1

One possible solution is setting ContentType and then writing binary directory to HttpServletResponse like this:

get("/image") {
  contentType  = "image/png"
  val out = response.getOutputStream

  // write binary to OutputStream here
  ...

  // Prevent updating response headers in following processes
  response.flushBuffer()

  () // return Unit
}

By the way, I tried fixing this problem several years ago, but it has been reverted due to the backwards compatibility: https://github.com/scalatra/scalatra/pull/332

Naoki Takezoe
  • 471
  • 2
  • 7