Skip to content

Commit 9f95125

Browse files
committed
Attempt to fix message #10834 to handle response to an HTTP request.
New behavior: by default the body of the response is considered as a string (it is the case when the answer is an XML), only if the header contain "content-type" containing json then the body is transformed into a map.
1 parent 363e912 commit 9f95125

File tree

1 file changed

+14
-4
lines changed
  • ummisco.gama.network/src/ummisco/gama/network/httprequest/utils

1 file changed

+14
-4
lines changed

ummisco.gama.network/src/ummisco/gama/network/httprequest/utils/Utils.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,25 @@ public static IMap<String, Object> formatResponse(final HttpResponse<String> res
3838

3939
try {
4040
responseMap = GamaMapFactory.create();
41-
4241
responseMap.put("CODE", response.statusCode());
4342

44-
Object jsonBody = "".equals(response.body()) ? "" : Jsoner.deserialize(response.body());
45-
responseMap.put("BODY", jsonBody);
46-
4743
IMap<String, List<String>> mapHeaders =
4844
GamaMapFactory.wrap(Types.STRING, Types.STRING, false, response.headers().map());
4945
responseMap.put("HEADERS", mapHeaders);
46+
47+
Object jsonBody = "";
48+
if( ! ("".equals(response.body()))) {
49+
List<String> contentType = mapHeaders.get("content-type");
50+
if(contentType != null) {
51+
if(contentType.stream().anyMatch(e -> e.contains("json"))) {
52+
jsonBody = Jsoner.deserialize(response.body());
53+
} else {
54+
jsonBody = response.body();
55+
}
56+
}
57+
}
58+
responseMap.put("BODY", jsonBody);
59+
5060
} catch (DeserializationException e) {
5161
e.printStackTrace();
5262
}

0 commit comments

Comments
 (0)