Showing
4 changed files
with
35 additions
and
48 deletions
| @@ -104,6 +104,9 @@ class GetHttpClient { | @@ -104,6 +104,9 @@ class GetHttpClient { | ||
| 104 | 'multipart/form-data; boundary=${body.boundary}'; | 104 | 'multipart/form-data; boundary=${body.boundary}'; |
| 105 | } else if (body is Map || body is List) { | 105 | } else if (body is Map || body is List) { |
| 106 | var jsonString = json.encode(body); | 106 | var jsonString = json.encode(body); |
| 107 | + | ||
| 108 | + bodyBytes = utf8.encode(jsonString); | ||
| 109 | + headers['content-length'] = bodyBytes.length.toString(); | ||
| 107 | headers['content-type'] = contentType ?? defaultContentType; | 110 | headers['content-type'] = contentType ?? defaultContentType; |
| 108 | //TODO check this implementation | 111 | //TODO check this implementation |
| 109 | if (contentType != null) { | 112 | if (contentType != null) { |
| @@ -112,9 +115,6 @@ class GetHttpClient { | @@ -112,9 +115,6 @@ class GetHttpClient { | ||
| 112 | jsonString = '$paramName=${Uri.encodeQueryComponent(jsonString)}'; | 115 | jsonString = '$paramName=${Uri.encodeQueryComponent(jsonString)}'; |
| 113 | } | 116 | } |
| 114 | } | 117 | } |
| 115 | - | ||
| 116 | - bodyBytes = utf8.encode(jsonString); | ||
| 117 | - headers['content-length'] = bodyBytes.length.toString(); | ||
| 118 | } else if (body == null) { | 118 | } else if (body == null) { |
| 119 | headers['content-type'] = contentType ?? defaultContentType; | 119 | headers['content-type'] = contentType ?? defaultContentType; |
| 120 | headers['content-length'] = '0'; | 120 | headers['content-length'] = '0'; |
| 1 | +import 'dart:convert'; | ||
| 2 | + | ||
| 3 | +import '../../../../get_core/get_core.dart'; | ||
| 4 | + | ||
| 5 | +import '../request/request.dart'; | ||
| 6 | + | ||
| 7 | +T bodyDecoded<T>(Request<T> request, String stringBody) { | ||
| 8 | + T body; | ||
| 9 | + var bodyToDecode; | ||
| 10 | + try { | ||
| 11 | + bodyToDecode = jsonDecode(stringBody); | ||
| 12 | + } on FormatException catch (_) { | ||
| 13 | + Get.log('Cannot decode body in json'); | ||
| 14 | + bodyToDecode = stringBody; | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + try { | ||
| 18 | + if (request.decoder == null) { | ||
| 19 | + body = bodyToDecode as T; | ||
| 20 | + } else { | ||
| 21 | + body = request.decoder(bodyToDecode); | ||
| 22 | + } | ||
| 23 | + } on Exception catch (_) { | ||
| 24 | + body = stringBody as T; | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + return body; | ||
| 28 | +} |
| 1 | import 'dart:async'; | 1 | import 'dart:async'; |
| 2 | -import 'dart:convert'; | ||
| 3 | import 'dart:html' as html; | 2 | import 'dart:html' as html; |
| 4 | import 'dart:typed_data'; | 3 | import 'dart:typed_data'; |
| 5 | - | ||
| 6 | import '../certificates/certificates.dart'; | 4 | import '../certificates/certificates.dart'; |
| 7 | import '../exceptions/exceptions.dart'; | 5 | import '../exceptions/exceptions.dart'; |
| 8 | import '../request/request.dart'; | 6 | import '../request/request.dart'; |
| 9 | import '../response/response.dart'; | 7 | import '../response/response.dart'; |
| 8 | +import 'body_decoder.dart'; | ||
| 10 | import 'request_base.dart'; | 9 | import 'request_base.dart'; |
| 11 | 10 | ||
| 12 | /// A `dart:html` implementation of `HttpRequestBase`. | 11 | /// A `dart:html` implementation of `HttpRequestBase`. |
| @@ -29,24 +28,6 @@ class HttpRequestImpl implements HttpRequestBase { | @@ -29,24 +28,6 @@ class HttpRequestImpl implements HttpRequestBase { | ||
| 29 | var bytes = await request.bodyBytes.toBytes(); | 28 | var bytes = await request.bodyBytes.toBytes(); |
| 30 | html.HttpRequest xhr; | 29 | html.HttpRequest xhr; |
| 31 | 30 | ||
| 32 | - // if (request.files != null) { | ||
| 33 | - // var data = html.FormData(); | ||
| 34 | - // if (request.files != null) { | ||
| 35 | - // for (MultipartFile element in request.files) { | ||
| 36 | - // var stream = element.finalize(); | ||
| 37 | - // data.appendBlob(element., html.File(element.finalize(), | ||
| 38 | - // element.filename), | ||
| 39 | - // element.filename); | ||
| 40 | - // } | ||
| 41 | - // } | ||
| 42 | - | ||
| 43 | - // xhr = await html.HttpRequest.request('${request.url}', | ||
| 44 | - // method: request.method, sendData: data); | ||
| 45 | - // } else { | ||
| 46 | - // xhr = html.HttpRequest() | ||
| 47 | - // ..open(request.method, '${request.url}', async: true); | ||
| 48 | - // } | ||
| 49 | - | ||
| 50 | xhr = html.HttpRequest() | 31 | xhr = html.HttpRequest() |
| 51 | ..open(request.method, '${request.url}', async: true); // check this | 32 | ..open(request.method, '${request.url}', async: true); // check this |
| 52 | 33 | ||
| @@ -68,19 +49,7 @@ class HttpRequestImpl implements HttpRequestBase { | @@ -68,19 +49,7 @@ class HttpRequestImpl implements HttpRequestBase { | ||
| 68 | final stringBody = | 49 | final stringBody = |
| 69 | await bodyBytesToString(bodyBytes, xhr.responseHeaders); | 50 | await bodyBytesToString(bodyBytes, xhr.responseHeaders); |
| 70 | 51 | ||
| 71 | - T body; | ||
| 72 | - try { | ||
| 73 | - if (request.decoder == null) { | ||
| 74 | - body = jsonDecode(stringBody) as T; | ||
| 75 | - } else { | ||
| 76 | - body = request.decoder(jsonDecode(stringBody)); | ||
| 77 | - } | ||
| 78 | - // body = request.decoder(stringBody); | ||
| 79 | - } on Exception catch (_) { | ||
| 80 | - body = stringBody as T; | ||
| 81 | - } | ||
| 82 | - | ||
| 83 | - // final body = jsonDecode(stringBody); | 52 | + final body = bodyDecoded<T>(request, stringBody); |
| 84 | 53 | ||
| 85 | final response = Response<T>( | 54 | final response = Response<T>( |
| 86 | bodyBytes: bodyBytes, | 55 | bodyBytes: bodyBytes, |
| 1 | -import 'dart:convert'; | ||
| 2 | import 'dart:io' as io; | 1 | import 'dart:io' as io; |
| 3 | 2 | ||
| 4 | import '../certificates/certificates.dart'; | 3 | import '../certificates/certificates.dart'; |
| 5 | import '../exceptions/exceptions.dart'; | 4 | import '../exceptions/exceptions.dart'; |
| 6 | import '../request/request.dart'; | 5 | import '../request/request.dart'; |
| 7 | import '../response/response.dart'; | 6 | import '../response/response.dart'; |
| 7 | +import 'body_decoder.dart'; | ||
| 8 | import 'request_base.dart'; | 8 | import 'request_base.dart'; |
| 9 | 9 | ||
| 10 | /// A `dart:io` implementation of `HttpRequestBase`. | 10 | /// A `dart:io` implementation of `HttpRequestBase`. |
| @@ -50,19 +50,9 @@ class HttpRequestImpl extends HttpRequestBase { | @@ -50,19 +50,9 @@ class HttpRequestImpl extends HttpRequestBase { | ||
| 50 | }); | 50 | }); |
| 51 | 51 | ||
| 52 | final bodyBytes = BodyBytes(response); | 52 | final bodyBytes = BodyBytes(response); |
| 53 | - | ||
| 54 | final stringBody = await bodyBytesToString(bodyBytes, headers); | 53 | final stringBody = await bodyBytesToString(bodyBytes, headers); |
| 55 | 54 | ||
| 56 | - T body; | ||
| 57 | - try { | ||
| 58 | - if (request.decoder == null) { | ||
| 59 | - body = jsonDecode(stringBody) as T; | ||
| 60 | - } else { | ||
| 61 | - body = request.decoder(jsonDecode(stringBody)); | ||
| 62 | - } | ||
| 63 | - } on Exception catch (_) { | ||
| 64 | - body = stringBody as T; | ||
| 65 | - } | 55 | + final body = bodyDecoded<T>(request, stringBody); |
| 66 | 56 | ||
| 67 | return Response( | 57 | return Response( |
| 68 | headers: headers, | 58 | headers: headers, |
-
Please register or login to post a comment