Home > Mobile >  I cant send image to api in flutter
I cant send image to api in flutter

Time:09-30

this is my upload function

void upload() async {
    print("inside upload image");
    if (file == null) return;
    String base64 = base64Encode(file.readAsBytesSync());
    String imagename = file.path.split("/").last;
    dynamic data = {"imagename": imagename, "image64": base64};
    var url = "http://waaasil.com/care/api/saveProfileImage";

    try {
      response1 = Dio().post(
        "***********************",
        data: data,
        options: Options(
            followRedirects: false,
            validateStatus: (status) {
              return status < 500;
            }),
      );
     
    } on DioError catch (e, s) {
      print("what happend  when upload image   ? :    "   e.toString());
    }
   

   
  }

when try to upload an image in the app the image showed successfully in the app < but when trying to send it to the api i get this error

enter image description here

CodePudding user response:

String imageName = file.path.split('/').last;

    FormData data = FormData.fromMap({
      "imagename": await MultipartFile.fromFile(
        file.path, filename: imageName),
    });

    Dio dio = new Dio();

    dio.options.connectTimeout =(60*1000); 
    dio.options.receiveTimeout= (60*1000);


    dio.post("http://waaasil.com/care/api/saveProfileImage", data: data,
        options: Options(
            followRedirects:false,
            validateStatus: (status)=> return status < 500,
        )).then((response) {
      
      if(response.statusCode==200){
        // response good
      }
      else //Something went wrong

If you have token(bearer token) then pass it on header

CodePudding user response:

error 500 means Internal Server Error in this case you need to talk with the backend to send you a valid documentation how to use the API is it multipart or JSON, post or put ... etc

  • Related