Skip to content

Commit

Permalink
Attempt to fix message #10834 to handle response to an HTTP request.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
benoitgaudou committed Jun 2, 2023
1 parent 363e912 commit 9f95125
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,25 @@ public static IMap<String, Object> formatResponse(final HttpResponse<String> res

try {
responseMap = GamaMapFactory.create();

responseMap.put("CODE", response.statusCode());

Object jsonBody = "".equals(response.body()) ? "" : Jsoner.deserialize(response.body());
responseMap.put("BODY", jsonBody);

IMap<String, List<String>> mapHeaders =
GamaMapFactory.wrap(Types.STRING, Types.STRING, false, response.headers().map());
responseMap.put("HEADERS", mapHeaders);

Object jsonBody = "";
if( ! ("".equals(response.body()))) {
List<String> contentType = mapHeaders.get("content-type");
if(contentType != null) {
if(contentType.stream().anyMatch(e -> e.contains("json"))) {
jsonBody = Jsoner.deserialize(response.body());
} else {
jsonBody = response.body();
}
}
}
responseMap.put("BODY", jsonBody);

} catch (DeserializationException e) {
e.printStackTrace();
}
Expand Down

0 comments on commit 9f95125

Please sign in to comment.