flutterhttpdart

ClientException (Content size exceeds specified contentLength. 10911 bytes written while expected 5965


i am getting ClientException (Content size exceeds specified contentLength. 10911 bytes written while expected 5965. error on trying to upload m4a file to my server. i tried to send the exact same request from postman and it works just fine. i also was able to send imgs and videos using the exact same request in flutter. i tracked the send method and it gives the error in the method 'send' in the Client class in the framework this is my request code:

static Future<String> uploadFile(
      {Attachment attachment,
      List<Attachment> attachments,
      String toUserIdForMessage}) async {
    Uri _uri = Uri.parse(globalUrl + file + 'Uploads');
    http.MultipartRequest _reqss = http.MultipartRequest(
      'POST',
      _uri,
    );
    Attachment _attForHeaders = attachment ?? attachments[0];
    _reqss.headers.addAll(await headersWToken);
    _reqss.fields['ownerType'] =
        _attForHeaders.attachmentOwner.index.toString();
    _reqss.fields['ownerId'] = _attForHeaders.ownerId.toString();
    _reqss.fields['toUserIdForMessage'] = toUserIdForMessage;
    if (attachment != null && attachment.path != null)
      _reqss.files.add(
        await http.MultipartFile.fromPath(
          attachment.path,
          attachment.path,
        ),
      );
    if (attachments != null && attachments.length != 0) {
      for (Attachment att in attachments) {
        _reqss.files.add(
          await http.MultipartFile.fromPath(
            att.path,
            att.path,
          ),
        );
      }
    }
    var _response = await (_reqss.send());
    var _re = _response.stream.bytesToString();
    return _re;
  }

Solution

  • this error has occured when i was trying to record a voice message and upload it. the problem was about not closing the recorder. so when the request is being prepared the recorder continued to record and thus appeared the size difference problem.