From 3c0fbf784f6f9376f3830fb821ceeb99f664f988 Mon Sep 17 00:00:00 2001 From: Marcel Date: Mon, 20 Jul 2020 13:43:55 +0200 Subject: [PATCH] [Requests] Add try_catch for utf8 decoding because dart really seems to have issues with json encoding and decoding. (also use camelCase) Took 18 minutes --- lib/matrix_api/matrix_api.dart | 9 +++++++-- lib/src/client.dart | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/matrix_api/matrix_api.dart b/lib/matrix_api/matrix_api.dart index 064417a..770dd25 100644 --- a/lib/matrix_api/matrix_api.dart +++ b/lib/matrix_api/matrix_api.dart @@ -193,8 +193,13 @@ class MatrixApi { ); break; } - var resp_body = utf8.decode(resp.bodyBytes); - var jsonString = String.fromCharCodes(resp_body.runes); + var respBody = resp.body; + try { + respBody = utf8.decode(resp.bodyBytes); + } catch (_) { + // No-OP + } + var jsonString = String.fromCharCodes(respBody.runes); if (jsonString.startsWith('[') && jsonString.endsWith(']')) { jsonString = '\{"chunk":$jsonString\}'; } diff --git a/lib/src/client.dart b/lib/src/client.dart index 3d552f5..bdd2ada 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -231,8 +231,13 @@ class Client { ) async { final response = await http .get('https://${MatrixIdOrDomain.domain}/.well-known/matrix/client'); - var resp_body = utf8.decode(response.bodyBytes); - final rawJson = json.decode(resp_body); + var respBody = response.body; + try { + respBody = utf8.decode(response.bodyBytes); + } catch (_) { + // No-OP + } + final rawJson = json.decode(respBody); return WellKnownInformations.fromJson(rawJson); }