Search code examples
javaspring-bootmime-types

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


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"
  }
}

Solution

  • 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.