[JAVA][apache-httpclient] Use `EntityUtils#toString instead of Scanner` (#17998)

``EntityUtils#toString`` automatically selects the correct encoding based on the received request.
Scanner currently uses the JVM default encoding, which doesn't always work.
This commit is contained in:
Alex B
2024-03-02 14:18:49 +01:00
committed by GitHub
parent cdf8973999
commit 009fda5e3d
3 changed files with 6 additions and 12 deletions

View File

@@ -771,8 +771,7 @@ public class ApiClient extends JavaTimeFormatter {
if (mimeType == null || isJsonMime(mimeType)) {
// Assume json if no mime type
// convert input stream to string
java.util.Scanner s = new java.util.Scanner(entity.getContent()).useDelimiter("\\A");
String content = (String) (s.hasNext() ? s.next() : "");
String content = EntityUtils.toString(entity);
if ("".equals(content)) { // returns null for empty body
return null;
@@ -781,8 +780,7 @@ public class ApiClient extends JavaTimeFormatter {
return objectMapper.readValue(content, valueType);
} else if ("text/plain".equalsIgnoreCase(mimeType)) {
// convert input stream to string
java.util.Scanner s = new java.util.Scanner(entity.getContent()).useDelimiter("\\A");
return (T) (s.hasNext() ? s.next() : "");
return (T) EntityUtils.toString(entity);
} else {
throw new ApiException(
"Deserialization for content type '" + mimeType + "' not supported for type '" + valueType + "'",