Home > Back-end >  Flutter post a list and image to django rest api
Flutter post a list and image to django rest api

Time:05-30

I want to send a post request to Django rest API from flutter with an image and some list of data as the body. Should I do it in one request or multiple requests? Can anyone help to solve this issue? Share any useful resource

CodePudding user response:

you can easily use dio package for multi part request. Here is repo, i think this will help you. just change ta field name

class ImageRepository {

Future<dynamic> uploadImage(filepath) async {
 
FormData formData = FormData.fromMap({
  "server_except_file_name": await MultipartFile.fromFile(filepath,
      filename: filepath.split('/').last),
   "server_except_list_name": yourList.toList(); 
});

var response = await Dio().post(
  url,
  data: formData),
);

print(response.data);
if (response.data['success'] == true) {
  return 'Image Upload';
} else {
  throw Exception 'Problem occor';
}
}

CodePudding user response:

Use multipart request, where you can combine a file with another object/json.

  • Related