httpcharacter-encodinghttpclienthttp2charset

How can i know the charset encoding of a http response?


This question is about charset and is NOT about content-type.

When I make a request to this page - https://www.baeldung.com/java-char-encoding, when I inspect the response, I can see the charset used for the response.
charset as utf-8
charset as utf-8

But when I make request to one of my internal server, i can not find charset info in response. no charset info

Knowing this info is important for me so I can set the encoding type of my HttpClient to same.

  1. Now how can i know using which charset the application/json data is encoded. ( i have no contact with the people maintaining the backend. So I have no real person to ask about this)
  2. Or not having this info means some default charset is used.
  3. If a default is used, does it depend on the http version - 1.1 or 2.0 etc.
  4. Any other info I should know/consider

(As these are related Questions, I have ask them in one post. Thanks for understanding.)


Solution

  • The content-type application/json has an assumed charset of UTF-8, as specified in RFC 8259.

    There may be other well-known content types that have an assumed charset, but text/html or text/javascript are not among these, so it must always be specified.

    This is why when you make a request that returns an HTML page or a JavaScript file the charset is specified, while when you make a request that returns JSON it is not.

    For the cases where the charset is not assumed and it's not specified, then you have to guess, and likely the guess could be wrong, and you get garbled characters.

    The charset does not depend on the HTTP protocol version.