From 01324927103a4d7093bcceb8bbfbb5cb4b8b4b93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Fri, 1 Sep 2023 21:21:58 +0100 Subject: [PATCH] :bug: fix #492 chopper base.dart send() authenticate sending wrong request --- chopper/lib/src/base.dart | 7 ++----- chopper/lib/src/interceptor.dart | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/chopper/lib/src/base.dart b/chopper/lib/src/base.dart index e791c13b..504590e2 100644 --- a/chopper/lib/src/base.dart +++ b/chopper/lib/src/base.dart @@ -309,11 +309,8 @@ base class ChopperClient { dynamic res = Response(response, response.body); if (authenticator != null) { - final Request? updatedRequest = await authenticator!.authenticate( - request, - res, - request, - ); + final Request? updatedRequest = + await authenticator!.authenticate(req, res, request); if (updatedRequest != null) { res = await send( diff --git a/chopper/lib/src/interceptor.dart b/chopper/lib/src/interceptor.dart index 1431fc23..0e6e2bba 100644 --- a/chopper/lib/src/interceptor.dart +++ b/chopper/lib/src/interceptor.dart @@ -193,9 +193,12 @@ class JsonConverter implements Converter, ErrorConverter { Request encodeJson(Request request) { final String? contentType = request.headers[contentTypeKey]; - return (contentType?.contains(jsonHeaders) ?? false) - ? request.copyWith(body: json.encode(request.body)) - : request; + if ((contentType?.contains(jsonHeaders) ?? false) && + (request.body.runtimeType != String || !_isJson(request.body))) { + return request.copyWith(body: json.encode(request.body)); + } + + return request; } FutureOr decodeJson(Response response) async { @@ -255,6 +258,15 @@ class JsonConverter implements Converter, ErrorConverter { static Request requestFactory(Request request) => const JsonConverter().convertRequest(request); + + static bool _isJson(dynamic data) { + try { + json.decode(data); + return true; + } catch (_) { + return false; + } + } } /// A [Converter] implementation that converts only [Request]s having a [Map] as their body.