Skip to content

Commit

Permalink
[HTTP client] Force encode to utf-8
Browse files Browse the repository at this point in the history
  • Loading branch information
proninyaroslav committed Aug 10, 2024
1 parent e5c6b00 commit 542e028
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/core/model/http/http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
// You should have received a copy of the GNU General Public License
// along with LibreTrack. If not, see <http://www.gnu.org/licenses/>.

import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:http/http.dart' as http;
Expand Down Expand Up @@ -50,20 +52,28 @@ class HttpResponse with _$HttpResponse {
@Injectable(as: HttpClient)
class HttpClientImpl implements HttpClient {
final http.Client _client;
final decoder = const Utf8Decoder();

HttpClientImpl(this._client);

@override
Future<HttpResponse> send(ServiceRequest request) async {
try {
final response = await _send(request);
late final String body;
try {
body = decoder.convert(response.body.codeUnits);
} on FormatException {
body = response.body;
}

if (response.statusCode >= 400) {
return HttpResponse.httpError(
statusCode: response.statusCode,
body: response.body,
body: body,
);
} else {
return HttpResponse.success(body: response.body);
return HttpResponse.success(body: body);
}
} on Exception catch (e, stackTrace) {
return HttpResponse.exception(e, stackTrace: stackTrace);
Expand Down

0 comments on commit 542e028

Please sign in to comment.