decodeJson<BodyType, InnerType> method
FutureOr<Response>
decodeJson<
BodyType, InnerType>( - Response response
)
inherited
Implementation
FutureOr<Response> decodeJson<BodyType, InnerType>(Response response) async {
final List<String> supportedContentTypes = [jsonHeaders, jsonApiHeaders];
final String? contentType = response.headers[contentTypeKey];
var body = response.body;
if (supportedContentTypes.contains(contentType)) {
// If we're decoding JSON, there's some ambiguity in https://tools.ietf.org/html/rfc2616
// about what encoding should be used if the content-type doesn't contain a 'charset'
// parameter. See https://github.com/dart-lang/http/issues/186. In a nutshell, without
// an explicit charset, the Dart http library will fall back to using ISO-8859-1, however,
// https://tools.ietf.org/html/rfc8259 says that JSON must be encoded using UTF-8. So,
// we're going to explicitly decode using UTF-8... if we don't do this, then we can easily
// end up with our JSON string containing incorrectly decoded characters.
body = utf8.decode(response.bodyBytes);
}
body = await tryDecodeJson(body);
if (isTypeOf<BodyType, Iterable<InnerType>>()) {
body = body.cast<InnerType>();
} else if (isTypeOf<BodyType, Map<String, InnerType>>()) {
body = body.cast<String, InnerType>();
}
return response.copyWith<BodyType>(body: body);
}