angel-dart / angel

[ARCHIVED] A polished, production-ready backend framework in Dart for the VM, AOT, and Flutter.
https://angel-dart.dev/
MIT License
1.06k stars 67 forks source link

HttpException: Unsupported contentTransferEncoding: binary #271

Closed insinfo closed 3 years ago

insinfo commented 4 years ago

This code for making a POST as multipart/form-data works well, but whan a body field contains characters such as "é" "à" "ç" it throws this exception on backend on Angel Framework

Future<RestResponseGeneric<T>> uploadFiles(
    String apiEndPoint,
    List<File> files, {
    String topNode,
    Map<String, String> headers,
    Map<String, dynamic> body,
    Map<String, String> queryParameters,
    String basePath,
    String protocol,
    String hosting,
    int hostPort,
  }) async {
    try {
      var url = uri(
        apiEndPoint,
        queryParameters: queryParameters,
        protocol: protocol,
        host: hosting,
        port: hostPort,
        basePath: basePath,
      );

      var headersDefault = {    
         'Accept': 'application/json',      
      };
      var request = http.MultipartRequest('POST', url);

      if (headers != null) {
        request.headers.addAll(headers);
      } else {
        request.headers.addAll(headersDefault);
      }

      if (body != null) {
        request.fields['data'] = jsonEncode(body);
      }
      //&& files is File
      if (files != null) {
        var reader = FileReader();
        for (var file in files) {
          reader.readAsArrayBuffer(file);
          await reader.onLoadEnd.first;
          request.files.add(await http.MultipartFile.fromBytes('file[]', reader.result,
              contentType: MediaType('application', 'octet-stream'), filename: file.name));
        }
      } else {
        throw Exception('RestClientGeneric@uploadFiles files cannot be null');
      }

      //fields.forEach((k, v) => request.fields[k] = v);
      var streamedResponse = await request.send();
      var resp = await http.Response.fromStream(streamedResponse);
      var respJson = jsonDecode(resp.body);

      return RestResponseGeneric<T>(
        headers: resp.headers,
        data: respJson,
        message: 'Sucesso',
        status: RestStatus.SUCCESS,
        statusCode: 200,
      );
    } catch (e, stacktrace) {
      print('RestClientGeneric@uploadFiles exception: ${e} stacktrace: ${stacktrace}');
      return RestResponseGeneric(message: 'Erro ${e}', status: RestStatus.DANGER, statusCode: 400);
    }
  }

HttpException: Unsupported contentTransferEncoding: binary #0 new HttpMultipartFormDataImpl (package:http_server/src/http_multipart_form_data_impl.dart:36:7)↵#1 HttpMultipartFormDataImpl.parse (package:http_server/src/http_multipart_form_data_impl.dart:86:12)↵#2 HttpMultipartFormData.parse (package:http_server/src/http_multipart_form_data.dart:70:33)↵#3 RequestContext.parseBody. (package:angel_framework/src/core/request_context.dart:269:35)↵#4 _MapStream._handleData (dart:async/stream_pipe.dart:229:31)↵#5 _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:166:13)↵#6 _rootRunUnary (dart:async/zone.dart:1192:38)↵#7 _CustomZone.runUnary (dart:async/zone.dart:1085:19)↵#8 _CustomZone.runUnaryGuarded (dart:async/zone.dart:987:7)↵#9 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)↵#10 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:266:7)↵#11 _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:779:19)↵#12 _StreamController._add (dart:async/stream_controller.dart:655:7)↵#13 _StreamController.add (dart:async/stream_controller.dart:597:5)↵#14 BoundMultipartStream._parse (package:mime/src/bound_multipart_stream.dart:332:16)↵#15 new BoundMultipartStream.. (package:mime/src/bound_multipart_stream.dart:136:13)↵#16 _rootRunUnary (dart:async/zone.dart:1192:38)↵#17 _CustomZone.runUnary (dart:async/zone.dart:1085:19)↵#18 _CustomZone.runUnaryGuarded (dart:async/zone.dart:987:7)↵#19 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)↵#20 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:266:7)↵#21 _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:134:11)↵#22 _ForwardingStream._handleData (dart:async/stream_pipe.dart:100:10)↵#23 _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:166:13)↵#24 _rootRunUnary (dart:async/zone.dart:1192:38)↵#25 _CustomZone.runUnary (dart:async/zone.dart:1085:19)↵#26 _CustomZone.runUnaryGuarded (dart:async/zone.dart:987:7)↵#27 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)↵#28 _DelayedData.perform (dart:async/stream_impl.dart:594:14)↵#29 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:710:11)↵#30 _PendingEvents.schedule. (dart:async/stream_impl.dart:670:7)↵#31 _rootRun (dart:async/zone.dart:1180:38)↵#32 _CustomZone.run (dart:async/zone.dart:1077:19)↵#33 _CustomZone.runGuarded (dart:async/zone.dart:979:7)↵#34 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:1019:23)↵#35 _rootRun (dart:async/zone.dart:1184:13)↵#36 _CustomZone.run (dart:async/zone.dart:1077:19)↵#37 _CustomZone.runGuarded (dart:async/zone.dart:979:7)↵#38 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:1019:23)↵#39 _microtaskLoop (dart:async/schedule_microtask.dart:43:21)↵#40 _startMicrotaskLoop (dart:async/schedule_microtask.dart:52:5)↵#41 _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:118:13)↵#42 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:169:5)↵

{
   "is_error":true,
   "status_code":400,
   "message":"HttpException: Unsupported contentTransferEncoding: binary #0      new HttpMultipartFormDataImpl (package:http_server/src/http_multipart_form_data_impl.dart:36:7)\n#1      HttpMultipartFormDataImpl.parse (package:http_server/src/http_multipart_form_data_impl.dart:86:12)\n#2      HttpMultipartFormData.parse (package:http_server/src/http_multipart_form_data.dart:70:33)\n#3      RequestContext.parseBody.<anonymous closure> (package:angel_framework/src/core/request_context.dart:269:35)\n#4      _MapStream._handleData (dart:async/stream_pipe.dart:229:31)\n#5      _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:166:13)\n#6      _rootRunUnary (dart:async/zone.dart:1192:38)\n#7      _CustomZone.runUnary (dart:async/zone.dart:1085:19)\n#8      _CustomZone.runUnaryGuarded (dart:async/zone.dart:987:7)\n#9      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)\n#10     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:266:7)\n#11     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:779:19)\n#12     _StreamController._add (dart:async/stream_controller.dart:655:7)\n#13     _StreamController.add (dart:async/stream_controller.dart:597:5)\n#14     BoundMultipartStream._parse (package:mime/src/bound_multipart_stream.dart:332:16)\n#15     new BoundMultipartStream.<anonymous closure>.<anonymous closure> (package:mime/src/bound_multipart_stream.dart:136:13)\n#16     _rootRunUnary (dart:async/zone.dart:1192:38)\n#17     _CustomZone.runUnary (dart:async/zone.dart:1085:19)\n#18     _CustomZone.runUnaryGuarded (dart:async/zone.dart:987:7)\n#19     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)\n#20     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:266:7)\n#21     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:134:11)\n#22     _ForwardingStream._handleData (dart:async/stream_pipe.dart:100:10)\n#23     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:166:13)\n#24     _rootRunUnary (dart:async/zone.dart:1192:38)\n#25     _CustomZone.runUnary (dart:async/zone.dart:1085:19)\n#26     _CustomZone.runUnaryGuarded (dart:async/zone.dart:987:7)\n#27     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)\n#28     _DelayedData.perform (dart:async/stream_impl.dart:594:14)\n#29     _StreamImplEvents.handleNext (dart:async/stream_impl.dart:710:11)\n#30     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:670:7)\n#31     _rootRun (dart:async/zone.dart:1180:38)\n#32     _CustomZone.run (dart:async/zone.dart:1077:19)\n#33     _CustomZone.runGuarded (dart:async/zone.dart:979:7)\n#34     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1019:23)\n#35     _rootRun (dart:async/zone.dart:1184:13)\n#36     _CustomZone.run (dart:async/zone.dart:1077:19)\n#37     _CustomZone.runGuarded (dart:async/zone.dart:979:7)\n#38     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1019:23)\n#39     _microtaskLoop (dart:async/schedule_microtask.dart:43:21)\n#40     _startMicrotaskLoop (dart:async/schedule_microtask.dart:52:5)\n#41     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:118:13)\n#42     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:169:5)\n",
   "errors":[

   ]
}

request image

image

relation https://github.com/dart-lang/http_server/commit/1cf78534e681f21d75c2d47659d9f9249be3d039

https://github.com/dart-lang/http/issues/159

https://github.com/flutter/flutter/issues/16879

thiagosantoscunha commented 4 years ago

I am having the same error presented in question.