I am working to extract response charset in a java web app, where I am using Apache HTTP Client.
For example, one possible value obtained from "Content-Type" header is
text/html; charset=UTF-8
Then my code will extract all text after the "=" sign...
So the charset as extracted will be
UTF-8
I just wanted to know, is the above method for obtaining response charset correct? Or is there some scenario where the above code will not work? Is there something I am missing here?
The Charset value specifies to the browser how to decode the data when displaying the response. The CharsetName of Response.Charset must match the code page value or mixed characters are displayed in the browser.
When one makes a request to a URI, it returns a response. This Response object in terms of python is returned by requests.method(), method being – get, post, put, etc. Response is a powerful object with lots of functions and attributes that assist in normalizing data or creating ideal portions of code.
response.status_code returns a number that indicates the status (200 is OK, 404 is Not Found). response.request returns the request object that requested this response. response.reason returns a text corresponding to the status code. response.raise_for_status () returns an HTTPError object if an error has occurred during the process.
Some methods are most commonly used with response, such as response.json (), response.status_code, response.ok, etc. Requests is mostly used for making http request to APIs (Application Programming Interface). Some of commonly used response methods are discussed here –
The method provided by forty-two can work. But the method is deprecated, I find out that this website has a good example of method to find the charset.
HttpEntity entity = response.getEntity();
ContentType contentType = ContentType.getOrDefault(entity);
Charset charset = contentType.getCharset();
System.out.println("Charset = " + charset.toString());
Doesn't httpclient (or http core) already provide that functionality? Something like this:
HttpResponse response = ...
String charset = EntityUtils.getContentCharSet(response.getEntity());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With