http icon indicating copy to clipboard operation
http copied to clipboard

Sending a dynamic value along with multipart request

Open AmmarMohamed21 opened this issue 4 years ago • 4 comments

I am trying to use multipart request to upload images and videos however I want to send a Map<String, Dynamic> but you can only use Map<String, String> which is a big limitation

This is the body I'm trying to send with the request:

bodyMap = {
  'type' : 'text',
  'content': [
     {
        'type': image'
        'identifier: '1234'
     }
   ]
};

and I try to send it to the request as following:

var request = http.MultipartRequest('POST', Uri.parse(url));
request.fields.addAll(bodyMap);

however it refuses as it only accepts Map<String, String>

AmmarMohamed21 avatar Nov 19 '21 18:11 AmmarMohamed21

Me too, How to fix it?

SittiphanSittisak avatar Feb 02 '22 14:02 SittiphanSittisak

@SittiphanSittisak I used Dio Package instead it allows you to do use Map<String, Dynamic> for example:

var formData = FormData.fromMap({
  'name': 'wendux',
  'age': 25,
  'file': await MultipartFile.fromFile('./text.txt', filename: 'upload.txt'),
  'files': [
    await MultipartFile.fromFile('./text1.txt', filename: 'text1.txt'),
    await MultipartFile.fromFile('./text2.txt', filename: 'text2.txt'),
  ]
});
var response = await dio.post('/info', data: formData);

AmmarMohamed21 avatar Feb 02 '22 15:02 AmmarMohamed21

@AmmarMohamed21 Thank you so much. I fix it by json_decode in API.

SittiphanSittisak avatar Feb 03 '22 04:02 SittiphanSittisak