Flutter multipart request along name value pair to server

3,526

I just had a problem similar to this. I had to upload an image along a key-value pair. I realize that I could upload the key-value pair using request.fields:

var request = new http.MultipartRequest("POST", uri);

request.fields['description'] = 'description';
request.fields['File-Name'] = 'FILENAME.jpg';
request.fields['qr_size'] = '3';
Share:
3,526
sathish kumar
Author by

sathish kumar

Updated on December 11, 2022

Comments

  • sathish kumar
    sathish kumar over 1 year

    I have to send to multipart request to server along with name value pair to server i mean id along with it. Can you please help me how to send id along with multipart files.

    Upload() async {
      var stream = new https.ByteStream(DelegatingStream.typed(Files[0].openRead()));
      var length = await Files[0].length();
    
    
      var stream1 = new https.ByteStream(DelegatingStream.typed(Files[1].openRead()));
      var length1 = await Files[1].length();
    
      var stream2 = new https.ByteStream(DelegatingStream.typed(Files[2].openRead()));
      var length2 = await Files[2].length();
    
    
    
      var uri = Uri.parse(openurl);
    
      var request = new https.MultipartRequest("POST", uri);
      var multipartFile1 = new https.MultipartFile('XX', stream, length,
          filename: basename(Files[0].path));
      var multipartFile2 = new https.MultipartFile('YY', stream1, length1,
          filename: basename(Files[0].path));
    
      var multipartFile3 = new https.MultipartFile('ZZ', stream2, length2,
          filename: basename(Files[0].path));
    
    
    
      //contentType: new MediaType('image', 'png'));
    
      request.files.add(multipartFile1);
      request.files.add(multipartFile2);
      request.files.add(multipartFile3);
      request.fields.addAll(other)
    
      var response = await request.send();
      print(response.statusCode);
      response.stream.transform(utf8.decoder).listen((value) {
        print(value);
      });
    }
    

    Thanks in advance

    Sathish

    • Egor Zvonov
      Egor Zvonov almost 5 years
      For HTTP requests I'd recommend using Dio library. It will help you avoid limitations of standard networking libs.