getResponseWithCustomDecoder method

Response getResponseWithCustomDecoder(
  1. Response response
)

Implementation

http.Response getResponseWithCustomDecoder(http.Response response) {
  String decoded;
  try {
    final codeUnits = response.bodyBytes;
    decoded = encoding.decode(codeUnits);
  } catch (e) {
    decoded = response.body;
  }
  final newHeaders = response.headers.map((key, value) {
    if (RegExp(CONTENT_TYPE_HEADER, caseSensitive: false).hasMatch(key)) {
      String defaultContentType = value;
      final hasCharSet = RegExp("charset").hasMatch(defaultContentType);
      if (!hasCharSet) {
        return MapEntry(key, "$defaultContentType; charset=${encoding.name}");
      }
    }
    return MapEntry(key, value);
  });
  return http.Response(
    decoded,
    response.statusCode,
    headers: newHeaders,
    isRedirect: response.isRedirect,
    persistentConnection: response.persistentConnection,
    reasonPhrase: response.reasonPhrase,
    request: response.request,
  );
}