Skip to content

Commit

Permalink
🐛 fix lejard-h#492 chopper base.dart send() authenticate sending wron…
Browse files Browse the repository at this point in the history
…g request
  • Loading branch information
techouse committed Sep 1, 2023
1 parent e706150 commit 0132492
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
7 changes: 2 additions & 5 deletions chopper/lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<BodyType, InnerType>(
Expand Down
18 changes: 15 additions & 3 deletions chopper/lib/src/interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response> decodeJson<BodyType, InnerType>(Response response) async {
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 0132492

Please sign in to comment.