How to remove charset=UTF-8 from Content-Type response header in Spring boot(v2.4.2)

Viewed 1430

I'm trying to download a pdf file from s3 and then return a response as byte array. Everything is working find except the "Content-Type", in Content-Type the charset=UTF-8 is getting added.

I have included "spring.http.encoding.force=false" in application.properties. Below is my code snipet:

@GetMapping(value = "/view/{clientId}/{documentId}", produces = { "application/pdf" })
public ResponseEntity<?> downloadDocument(@PathVariable String clientId,
                                              @PathVariable String documentId) {

....
return ResponseEntity.ok()
                    .contentLength(...)
                    .header(HttpHeaders.CONTENT_TYPE, "application/pdf")
                    .header(HttpHeaders.CONTENT_DISPOSITION,"inline; filename=\"download.pdf\"")
                    .body(downloadInputStream.toByteArray());
}

In the response log I'm getting following:

{
  "type": "response",
  "correlation": "e297f48c4475d510",
  "direction": "EGRESS",
  "duration": 5536,
  "protocol": "HTTP/1.1",
  "status": 200,
  "headers": {
    "Keep-Alive": "timeout=360",
    "x-request-id": "e297f48c4475d510",
    "Content-Disposition": "inline; filename=\"download.pdf\"",
    "Connection": "keep-alive",
    "Content-Length": "864038",
    "Date": "Thu, 01 Jul 2021 09:53:08 GMT",
    "Content-Type": "application/pdf;charset=UTF-8"
  }
}
2 Answers

Have you try below properties?

server.servlet.encoding.force=false
server.servlet.encoding.forceResponse=false

However, both of the properties should default to false, so unless it is explicitly set to true, it won't set the encoding for you.

Another posibility is that HttpServletResponse.setCharacterEncoding() is called somewhere in your code or library. You can try to debug on that method to see if any class is calling it.

charset=UTF-8 is usually for plain text simple pdf files and because there is no "Content-Transfer-Encoding" the server is just assuming. inline is load and use , attachment pops a dialog box. Page 2 and 3 in the link show how to properly set a servlet download. http://www.windsolarhybridaustralia.x10.mx/Ramfile-html_B_.pdf

Related