Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why dio posts empty form data?

Tags:

http

flutter

dart

I have a function to upload an image but the server does not receive anything and I get 500 status code. I'm sure that the server is fine. It works when I send a post request from the postman!
This is my function:

uploadPrescriptionToAll(File file, data) async {

  String convertedFilePath = await convertImage(file);

   String token = await getToken();

   Response response;
   Dio dio = Dio();
   dio.options.baseUrl = "http://x.x.x.x:x";

   FormData formData = FormData.from({
     "image":
         UploadFileInfo(new File(convertedFilePath), "image.jpg"),
     "data": data,
   });
   try {
     response = await dio.post("/api/images",
         data: formData,
         options: Options(headers: {
           "Authorization": token,
           "Content-Type": "multipart/form-data"
         }));
   } catch (e) {
     print("Error Upload: " + e.toString());
   }
   print("Response Upload:" + response.toString());
}

how can I post the file (form-data) correctly? Is there another way to do it?

like image 774
Moein Hosseini Avatar asked Sep 02 '18 06:09

Moein Hosseini


1 Answers

Using Dio It's very simple by using : FormData.fromMap()

searchCityByName(String city) async {
     Dio dio = new Dio(); 
    var a = {"city": city};
    var res = await dio.post(apiSearchState, data:FormData.fromMap(a));
  }
like image 123
Deepak Ror Avatar answered Nov 03 '22 05:11

Deepak Ror