How to get the Content-Length from the MultipartRequest - Flutter

Viewed 600

I'm trying to post a data to the server and I'm successfully getting the true response from the server but the Items which i posted is not getting reflected in the server. But when i do the same request upon the postman it is getting posted and getting reflected in the server. So upon checkingsome of it i fould out that the Content-Type, Content-Length and the Host is mandatory from the postman request side but i have just implemented just the Content-Type in my code. So to give a try i was thinking like if i add the Content-Length and the Host probably it might reflect in the server but how to get that data?

Below is my code

var uri = Uri.parse(Constant.postAdUrl);
    Map<String, String> headers = {
      'Content-Type': 'multipart/form-data',
      "Accept": "application/json",
      'Content-Length' : ''
    };
    http.MultipartRequest request = new http.MultipartRequest('POST', uri);
    request.headers.addAll(headers);
    List<http.MultipartFile> newList = new List<http.MultipartFile>();
    for (int i = 0; i < images.length; i++) {
      var path =
          await FlutterAbsolutePath.getAbsolutePath(images[i].identifier);
      File imageFile = File(path);
      var streams = new http.ByteStream(imageFile.openRead());
      var lengths = await imageFile.length();
      request.files.add(await new http.MultipartFile(
          'files[]', streams, lengths,
          filename: _image.path));
    }
    if (_image != null) {
      request.files.add(await new http.MultipartFile('adcover', stream, length,
          filename: _image.path));
    }
    request.files.addAll(newList);
    request.fields['ads_userfkid'] = userId;
    request.fields['ads_name'] = widget.title;
    request.fields['ads_description'] = widget.description;
    request.fields['ads_price'] = widget.price;
    request.fields['ads_subcatfkid'] = widget.subId;
    request.fields['ads_location'] = widget.location;
    var response = await request.send();
    if (response.statusCode == 200) {
      Navigator.pushReplacement(
          context, MaterialPageRoute(builder: (context) => DashboardScreen()));
    } else {
      print("Upload Failed");
    }
  }
0 Answers
Related