Jonatas

separate jsonDecode from default decode

... ... @@ -104,6 +104,9 @@ class GetHttpClient {
'multipart/form-data; boundary=${body.boundary}';
} else if (body is Map || body is List) {
var jsonString = json.encode(body);
bodyBytes = utf8.encode(jsonString);
headers['content-length'] = bodyBytes.length.toString();
headers['content-type'] = contentType ?? defaultContentType;
//TODO check this implementation
if (contentType != null) {
... ... @@ -112,9 +115,6 @@ class GetHttpClient {
jsonString = '$paramName=${Uri.encodeQueryComponent(jsonString)}';
}
}
bodyBytes = utf8.encode(jsonString);
headers['content-length'] = bodyBytes.length.toString();
} else if (body == null) {
headers['content-type'] = contentType ?? defaultContentType;
headers['content-length'] = '0';
... ...
import 'dart:convert';
import '../../../../get_core/get_core.dart';
import '../request/request.dart';
T bodyDecoded<T>(Request<T> request, String stringBody) {
T body;
var bodyToDecode;
try {
bodyToDecode = jsonDecode(stringBody);
} on FormatException catch (_) {
Get.log('Cannot decode body in json');
bodyToDecode = stringBody;
}
try {
if (request.decoder == null) {
body = bodyToDecode as T;
} else {
body = request.decoder(bodyToDecode);
}
} on Exception catch (_) {
body = stringBody as T;
}
return body;
}
... ...
import 'dart:async';
import 'dart:convert';
import 'dart:html' as html;
import 'dart:typed_data';
import '../certificates/certificates.dart';
import '../exceptions/exceptions.dart';
import '../request/request.dart';
import '../response/response.dart';
import 'body_decoder.dart';
import 'request_base.dart';
/// A `dart:html` implementation of `HttpRequestBase`.
... ... @@ -29,24 +28,6 @@ class HttpRequestImpl implements HttpRequestBase {
var bytes = await request.bodyBytes.toBytes();
html.HttpRequest xhr;
// if (request.files != null) {
// var data = html.FormData();
// if (request.files != null) {
// for (MultipartFile element in request.files) {
// var stream = element.finalize();
// data.appendBlob(element., html.File(element.finalize(),
// element.filename),
// element.filename);
// }
// }
// xhr = await html.HttpRequest.request('${request.url}',
// method: request.method, sendData: data);
// } else {
// xhr = html.HttpRequest()
// ..open(request.method, '${request.url}', async: true);
// }
xhr = html.HttpRequest()
..open(request.method, '${request.url}', async: true); // check this
... ... @@ -68,19 +49,7 @@ class HttpRequestImpl implements HttpRequestBase {
final stringBody =
await bodyBytesToString(bodyBytes, xhr.responseHeaders);
T body;
try {
if (request.decoder == null) {
body = jsonDecode(stringBody) as T;
} else {
body = request.decoder(jsonDecode(stringBody));
}
// body = request.decoder(stringBody);
} on Exception catch (_) {
body = stringBody as T;
}
// final body = jsonDecode(stringBody);
final body = bodyDecoded<T>(request, stringBody);
final response = Response<T>(
bodyBytes: bodyBytes,
... ...
import 'dart:convert';
import 'dart:io' as io;
import '../certificates/certificates.dart';
import '../exceptions/exceptions.dart';
import '../request/request.dart';
import '../response/response.dart';
import 'body_decoder.dart';
import 'request_base.dart';
/// A `dart:io` implementation of `HttpRequestBase`.
... ... @@ -50,19 +50,9 @@ class HttpRequestImpl extends HttpRequestBase {
});
final bodyBytes = BodyBytes(response);
final stringBody = await bodyBytesToString(bodyBytes, headers);
T body;
try {
if (request.decoder == null) {
body = jsonDecode(stringBody) as T;
} else {
body = request.decoder(jsonDecode(stringBody));
}
} on Exception catch (_) {
body = stringBody as T;
}
final body = bodyDecoded<T>(request, stringBody);
return Response(
headers: headers,
... ...